gpt4 book ai didi

mysql - 如何在sql查询中放入条件

转载 作者:行者123 更新时间:2023-11-29 05:18:50 26 4
gpt4 key购买 nike

如果“flag”列等于“shown”,我想在分数表中显示所有得分记录,在学生表中显示学生姓名。否则,如果 Score 表中的“flag”列等于“hidden”,我将显示学生姓名 =“Student”的所有得分记录。最后按时间顺序显示所有内容。

这是我尝试过的:

select s.student_name if h.flag = 'shown', select 'student' as s.student_name if h.flag = 'hidden',
h.subject1_score,
h.subject2_score,
h.subject3_score,
h.subject4_score,
h.subject5_score,
h.exam_date
from Score_Table h
join Student s on h.student_id = u.student_id
order by h.exam_date

但这似乎行不通。我应该改变什么?谢谢!

最佳答案

您的 IF 语法已关闭。阅读http://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html

select if(h.flag = 'shown',s.student_name,'student') student_name,
h.subject1_score,
h.subject2_score,
h.subject3_score,
h.subject4_score,
h.subject5_score,
h.exam_date
from Score_Table h
join Student s on h.student_id = s.student_id
order by h.exam_date

关于mysql - 如何在sql查询中放入条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28874686/

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