我有3个查询,它们查询名为campaign
的同一表-
从广告系列中选择a.org_name,其中a.mm_id为NULL
从广告系列中选择a.org_name,其中a.mm_id为NULL并且a.active = 'Y'
从广告系列中选择a.org_name,其中a.mm_id为NULL并且a.active = 'N'AND a.xaction ='Y'
我需要将这三个查询合并为一个。
我希望输出看起来像这样
+----------+--------+--------+---------+
| org_name | failed | active | xaction |
+----------+--------+--------+---------+
| Pepsi | yes | no | yes |
| Xerox | no | no | yes |
+----------+--------+--------+---------+
答案 0 :(得分:1)
针对您在问题中指定的确切条件产生结果的查询将是这样的:
SELECT a.org_name,
CASE WHEN a.mm_id IS NULL THEN 'yes' ELSE 'no' END AS failed,
CASE WHEN a.mm_id IS NULL AND a.active = 'Y' THEN 'yes' ELSE 'no' END AS active,
CASE WHEN a.mm_id IS NULL AND a.xaction = 'Y' THEN 'yes' ELSE 'no' END AS xaction
FROM campaign a
您可以根据需要替换每个CASE
表达式中的条件。
答案 1 :(得分:0)
SELECT a.org_name,
IF(a.mm_id IS NULL,'yes','no') AS failed,
IF(a.mm_id IS NULL AND a.active = 'Y', 'yes', 'no') AS active,
IF(a.mm_id IS NULL AND a.xaction = 'Y','yes','no') AS xaction
FROM campaign a
IF
(是比较函数)和WHEN THEN
是解决问题的方法