本文介绍了在MySQL中仅选择偶数/奇数行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MySQL的表中选择所有偶数或奇数行,而不使用ID字段.我试过了,但是我想它不起作用,因为它基于SQL Server:如何仅显示偶数或奇数行在sql server 2008中?

I'm trying to select all even or odd rows from a table in MySQL without using the ID field.I tried this, but I suppose that it doesn't work since it's based on SQL Server:how to show only even or odd rows in sql server 2008?

谢谢大家.

推荐答案

假设您有一列指定表的顺序,那么您可以使用变量来完成所需的操作:

Assuming you have a column that specifies the ordering of the table, then you can use variables to do what you want:

select t.*
from (select t.*, (@rn := @rn + 1) as seqnum
      from table t cross join
           (select @rn := 0) vars
      order by col
     ) t
where mod(seqnum, 2) = 0;

这篇关于在MySQL中仅选择偶数/奇数行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:00