如果列“flag”等于“显示”,我想在学生表中显示得分表和学生姓名中的所有得分记录。否则,如果Score表中的“flag”列等于“hidden”,我将显示所有带有student name =“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
但这似乎不起作用。我应该改变什么? 谢谢!
答案 0 :(得分:2)
您的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
答案 1 :(得分:1)
cASE
我演变为,
CASE WHEN h.flag = 'shown'
它将检查f.flag='shown'
是否为真,然后它将从表中选择s.studentName
作为行值。如果案例为假,则会选择student
作为StudentName
。
您也可以将案例用作
select
CASE WHEN h.flag = 'shown' THEN s.student_name
ELSE 'student' END AS StudentName,
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