gpt4 book ai didi

mysql - SQL如何每次倒序选择一定数量的数据

转载 作者:行者123 更新时间:2023-11-29 10:34:17 26 4
gpt4 key购买 nike

如何从mysql数据库中每次逆序选择2条数据。例如,在此表中

|---------------------|------------------|
| id | category |
|---------------------|------------------|
| 1 | 1 |
|---------------------|------------------|
| 2 | 3 |
|---------------------|------------------|
| 3 | 1 |
|---------------------|------------------|
| 4 | 2 |
|---------------------|------------------|
| 5 | 1 |
|---------------------|------------------|
| 6 | 3 |
|---------------------|------------------|
| 7 | 1 |
|---------------------|------------------|

我想每次选择 2 个数据,其中类别为 1,以相反的顺序。第一次运行查询时,我将获得 id =7 和 id =5 的数据。第二次运行查询时,我将获取 id =3 和 id =1 的数据。如何形成该查询?

最佳答案

您可以使用不同的(按限制)查询:

select id from [your-table-name-here] where category=1 order by id desc limit 0, 2 --to get first two
select id from [your-table-name-here] where category=1 order by id desc limit 2, 2 --to get next two
...
select id from [your-table-name-here] where category=1 order by id desc limit x, x+2 --etc

关于mysql - SQL如何每次倒序选择一定数量的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46759145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com