是可以在多个列上获得多个条件我尝试使用它...它没有创建任何错误但结果不正确。我应该使用IF吗?
SELECT
p.name,
p.is_deleted,
( CASE WHEN pro.is_active = 1 THEN 'Active'
WHEN pro.is_del = 1 THEN 'Not Active-Promotion Deleted'
WHEN p.is_active = 0 THEN 'Not Active-Page Inactive'
WHEN p.is_deleted = 1 THEN 'Not Active-Page Deleted'
END) AS 'Status'
FROM
page_promotions pro
INNER JOIN pages p
ON pro.page_id = p.pages_id
答案 0 :(得分:1)
CASE语句从头到尾进行评估。一旦案件得到满足,该声明将结束。您可能需要为每个案例添加其他子句,或对其进行重新排序,以获得所需的结果。
这是SQL的行为:
为每个案例添加一些附加条件或重新排序以获得正确的结果
在你的情况下得到欲望输出
SELECT
p.name,
p.is_deleted,
( CASE WHEN p.is_active = 0 THEN 'Not Active-Page Inactive'
WHEN p.is_deleted = 1 THEN 'Not Active-Page Deleted'
WHEN pro.is_del = 1 THEN 'Not Active-Promotion Deleted'
WHEN pro.is_active = 1 THEN 'Active'
END) AS 'Status'
FROM
page_promotions pro
INNER JOIN pages p
ON pro.page_id = p.pages_id