mysql 循环输出结果,MySQL select语句以循环方式返回结果

i am working with an app that via swipes you can see cards one after the other. Both left and right swipes are permitted. If the user swipes right he will see the next available card. If he swipes at the left he will see the previous card.

The cards details are stored to a MySQL table 'Cards' and have a unique id let's say 'CardID' starting from number 1.000.000 and so on. Let's say that i currently have added 500 cards to the 'Cards' table, so the last card ID stored is 1.000.500. I want to create a procedure that will send back to the user 20 cards at a time depending on the direction he keeps swipping. If the swipes are done to the left the procedure will send the previous 20 records and so on.

Now think that the user keeps swipping left and requests the previous records from number 1.000.009 and below. I want to write a select statement that will fetch and serve to the user those 9 rows left and the 11 rows from the bottom of the table alltogether. That would be records with CardID 1.000.489 to 1.000.500, so as the user swipes, when he will swipe no 1.000.000 (the first) he will go by circle to see 1.000.500 and vice versa

Thank you in advance

解决方案DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table (id SERIAL PRIMARY KEY);

INSERT INTO my_table VALUES

(1),

(2),

(3),

(4),

(5),

(6),

(7),

(8),

(9);

SELECT * FROM my_table ORDER BY id > 5 DESC, id;

+----+

| id |

+----+

| 6 |

| 7 |

| 8 |

| 9 |

| 1 |

| 2 |

| 3 |

| 4 |

| 5 |

+----+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值