php - MySql 从数据库中选择第二条和第三条记录

标签 php mysql

如何从 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/

相关文章:

php - Drupal View : Generate xml file

javascript - 如何将图像从数据库传递到javascript?

mysql - 在同一个表中插入记录后,程序中未完成更新

mysql - 按修改后的时间戳分组并从 Elasticsearch 返回最大值

mysql - 创建 View 检查其他表是否有值(value)

python - 将 Python 代码与 MySQL 连接时出现回溯错误

javascript - Facebook,在新窗口中发布标签以获取链接和 FORM

php - file_get_contents() 无法打开流 : No such file or directory

php - Wordpress - 使用元数据获取邮政编码并输出谷歌地图

mysql MBRWithin() 具有多个点