表主题:
+--------+----+
| name | id |
+--------|----+
| apple | 3 |
| banana | 1 |
| tree | 4 |
| horse | 6 |
| bird | 7 |
| raq | 2 |
+--------+----+
所以我尝试这样的输出(例如,如果我的$id_subject = 4
,我的下一行必须是5 -6 7-1 ....)
+--------+----+
| name | id |
+--------+----+
| tree | 4 |
| horse | 6 |
| bird | 7 |
| banana | 1 |
| raq | 2 |
| apple | 3 |
+--------+----+
我的查询:
select subject_url,subjects.id
from students_group
left join teacher_group on teacher_group.group_school_id = students_group.group_id
left join subjects on teacher_group.subject_id = subjects.id
where students_group.user_id = 83
ORDER BY subjects.id = 5 desc
但我得到了这个输出:
+--------+----+
| name | id |
+--------+----+
| tree | 4 |
| apple | 3 |
| banana | 1 |
| horse | 6 |
| bird | 7 |
| raq | 2 |
+--------+----+
如何让它按预期工作?
答案 0 :(得分:1)
尝试
ORDER BY subjects.id> = 5 desc,subjects.id asc
“subjects.id> = 5”将返回1或0,因此其行为true的行将首先出现,在这些行中,它们将按ID递增排序
答案 1 :(得分:0)
select subject_url,subjects.id
from students_group
left join teacher_group on teacher_group.group_school_id = students_group.group_id
left join subjects on teacher_group.subject_id = subjects.id
where students_group.user_id = 83
ORDER BY
(subjects.id >= 4) DESC
, subjects.id ASC
答案 2 :(得分:0)
ORDER BY
if (subjects.id=$id_subject, -1,
if (subjects.id>$id_subject, 0, 1)
), subjects.id