如何从 mysql 数据库中选择每第二条和第三条记录?
这是我正在使用的sql
此sql需要选择每隔一条记录:
$sql_1 = "SELECT * FROM test_db ORDER BY id DESC";
这个需要选择每第三条记录:
$sql_2 = "SELECT * FROM test_db ORDER BY id DESC";
我会尽力更好地解释我的问题。
http://pokit.org/get/img/f202a616d0b5e8fe4c6c5875b9a668be.jpg
这将是我的 mysql 数据库。数据库名称为 test_db。如何显示此数据库中的每条记录,其中 LIMIT 2
并按 DESC
排序?顺序需要像这样 (44,15,6)
最佳答案
您应该更清楚地定义“每秒记录”。只有有命令,每一秒才是合理的。
您是指id
吗?
那就这样了
select * from test_db where id % 2 == 0; // or id % 2 == 1
应该以其他顺序每秒一次吗?
然后我会选择(编辑:order by 需要位于子选择内)
set @counter:=0;
select * from
( select @counter:=@counter+1 as cnt, t.*
from test_db as t
order by inserted desc) as isel
where cnt % 2 = 0;
或者,如果您想按升序 id 排序以找到要丢弃的第一行,则使用
set @counter:=0;
select * from
( select @counter:=@counter+1 as cnt, t.*
from test_db as t
order by id) as isel
where cnt % 2 = 0
order by id desc;
或者,如果您想按 id 降序排序以找到要丢弃的第一行,则使用
set @counter:=0;
select * from
( select @counter:=@counter+1 as cnt, t.*
from test_db as t
order by id desc) as isel
where cnt % 2 = 0
;
我希望你能明白。
关于php - MySql 从数据库中选择第二条和第三条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32666660/