gpt4 book ai didi

mysql - 如何在mysql中按升序和降序修改一列

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:44 24 4
gpt4 key购买 nike

来自以下 Mysql 查询

select (
case
when grade < 8 then null
else name
end
) as name,
grade, marks
from students
join Grades
ON Min_mark = (SELECT MAX(Min_mark) from grades where min_mark <= students.marks)
order by grade desc,name asc

我想根据等级值对订单进行排序,即

生成包含三列的报告:名称、等级和标记。报告必须按年级降序排列——即首先输入较高的年级。如果分配给同一年级 (1-10) 的学生不止一名,则按姓名字母顺序对这些特定学生进行排序。最后,如果成绩低于8,则用“NULL”作为他们的名字,并按照分数从小到大的顺序排列

select (
case
when grade < 8 then null
else name
end
) as name,
grade, marks
from students
join Grades
ON Min_mark = (SELECT MAX(Min_mark) from grades where min_mark <= students.marks)
case
when grade <8 then
order by grade desc, name asc
else
order by marks asc
end

但是由于语法错误,第二个查询没有执行。有人可以建议我如何获得所需的结果。提前致谢

最佳答案

我的猜测是这样的:

ORDER BY grade > 10 DESC /* orders by grade>10 records first */
, CASE WHEN grade > 10 THEN grade ELSE null END DESC /* all grade<=10 records will "sort" equally among themselves */
, CASE WHEN grade > 10 THEN name ELSE marks END ASC /* all grade<=10 records will "sort" by `name`, others will sort my `marks` */

关于mysql - 如何在mysql中按升序和降序修改一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618479/

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