如何使用case语句来比较多列 [英] How to use case statement to compare multiple columns
问题描述
你好,
我有2张桌子:
1)桌面课程(id,姓名)
2)桌子预订(id,姓名)
大学的经理可以保留任何课程,
确保课程保留在哪里,然后系统应插入本课程的ID和名称预订表,
预订课程后
,
i想要显示课程名称附近的栏目:
if(id,name )到课程表包含在预订表的(ID,名称)然后'课程保留'否则'不保留'
i尝试不在声明,但它不适用于多个列
谢谢
Hello,
I have 2 tables :
1)table course (id,name)
2)table reservation (id,name)
The manager in the university can reserve any course,
sure where a course is reserved then the id and the name of this course should be inserted by the system in the reservation table,
after reserve courses,
i want to show a column near to the name of the course :
if (id,name) to the course table are contained in the (id,name) to the reservation table then 'Course Reserved' else 'Not Reserved'
i tried not in statement but it don't work on multiple columns
thanks
推荐答案
请阅读我对这个问题的评论。
我不确定你的要求,但试试这个:
Please, read my comments to the question.
I'm not sure your requirements, but try this:
SELECT c.ID AS CourseID, CASE WHEN r.ID IS NULL THEN 'Not reserved' ELSE 'Reserved' END AS ReservationID
FROM Course AS c LEFT JOIN Reservation AS r ON c.ID = r.ID
你可以与 COALESCE
函数类似的效果:
You can achive similar effect with COALESCE
function:
SELECT c.ID AS CourseID, COALESCE(r.ID, 'Not reserved') AS ReservationID
FROM Course AS c LEFT JOIN Reservation AS r ON c.ID = r.ID
请阅读这些文章:
SQL连接的可视化表示 [ ^ ]
CASE(SQL) [ ^ ]
COALESCE(SQL) [ ^ ]
Please, read these articles:
Visual Representation of SQL Joins[^]
CASE (SQL)[^]
COALESCE (SQL)[^]
这篇关于如何使用case语句来比较多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!