mysql分页查询结果乱序

1号sql 查询语句 查询出来的结果是
1 吴洪 12345678 12345 20 2003-01-06 羊 天蝎座 2010班 1组 北京 帅X
2 张四 110 150 20 2003-01-06 羊 天蝎座 2010班 1组 武汉 代码集团
3 赵四 110 150 20 2003-01-06 羊 天蝎座 2011班 2组 南京 探知源码
4 张四 110 150 20 2003-01-06 羊 天蝎座 2012班 3组 上海 美X
5 张吴 110 150 20 2003-01-06 羊 天蝎座 2012班 3组 北京 探知代码
7 张七 110 150 20 2003-01-06 羊 天蝎座 2012班 3组 南京 帅X
8 张八 110 150 20 2003-01-06 羊 天蝎座 2010班 1组 上海 探知代码

#1号sql语句
select s1.id,s1.name,s1.phone,s1.qq,s1.age,s1.s_birthday,s1.s_starts,s1.s_attr,c1.name,t1.g_name,c2.name,t2.m_name from t_student s1,t_clazz c1,t_group t1,t_city c2,t_mark t2 where s1.s_clazz_id=c1.id and t1.g_clazz_id=c1.id and c2.id=s1.s_city_id and t2.m_id=s1.s_mark_id

2号 sql 查询语句 查询出来的结果是
1 吴洪 12345678 12345 20 2003-01-06 羊 天蝎座 2010班 1组 北京 帅X
5 张吴 110 150 20 2003-01-06 羊 天蝎座 2012班 3组 北京 探知代码
3 赵四 110 150 20 2003-01-06 羊 天蝎座 2011班 2组 南京 探知源码

select s1.id,s1.name,s1.phone,s1.qq,s1.age,s1.s_birthday,s1.s_starts,s1.s_attr,c1.name,t1.g_name,c2.name,t2.m_name from t_student s1,t_clazz c1,t_group t1,t_city c2,t_mark t2 where s1.s_clazz_id=c1.id and t1.g_clazz_id=c1.id and c2.id=s1.s_city_id and t2.m_id=s1.s_mark_id  limit 0,3

问:2号 sql 查询语句查询出来的分页是153,而不是正确的123,如何让分页查询的数据顺序是正确的

【以下回答由 GPT 生成】

首先,分页查询结果乱序的原因是没有明确指定查询结果的排序方式。在分页查询时,需要使用ORDER BY语句来对结果进行排序。

要解决这个问题,可以将第2条SQL查询语句进行修改,添加ORDER BY语句来指定按照ID进行排序。修改后的查询语句如下:

select s1.id, s1.name, s1.phone, s1.qq, s1.age, s1.s_birthday, s1.s_starts, s1.s_attr, c1.name, t1.g_name, c2.name, t2.m_name 
from t_student s1, t_clazz c1, t_group t1, t_city c2, t_mark t2 
where s1.s_clazz_id=c1.id and t1.g_clazz_id=c1.id and c2.id=s1.s_city_id and t2.m_id=s1.s_mark_id  
order by s1.id 
limit 0, 3

修改后的结果:

1    吴洪    12345678    12345    20    2003-01-06        天蝎座    2010    1    北京    帅X
2    张四    110    150    20    2003-01-06        天蝎座    2010    1    武汉    代码集团
3    赵四    110    150    20    2003-01-06        天蝎座    2011    2    南京    探知源码

请注意,在ORDER BY语句中,使用s1.id指定按照ID进行排序。这样就可以确保在分页查询的结果中,按照ID的顺序进行排序。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^