我想计算来自同一列且具有相同 WHERE 条件的多个子字符串的出现次数。对于每个子字符串,我都有一个 UNION SELECT 语句,包括重复的 WHERE 条件。最终结果是使用 appox 进行的查询。2500行sql代码,但效果不错。
查询的简化示例:
SELECT ‘be’ AS country, count(destination) FROM demo
WHERE destination LIKE ‘%be%’ AND field_a=xzx AND field_b=bbb
UNION SELECT ‘de’ AS country, count(destination) FROM demo
WHERE destination LIKE ‘%de%’ AND field_a=xzx AND field_b=bbb
UNION SELECT ‘fr’ AS country, count(destination) FROM demo
WHERE destination LIKE ‘%fr%’ AND field_a=xzx AND field_b=bbb
UNION SELECT ‘nl’ AS country, count(destination) FROM demo
WHERE destination LIKE ‘%nl%’ AND field_a=xzx AND field_b=bbb
是否可以修改查询以使 WHERE 条件仅在查询中出现一次?
请通过此链接找到我的问题的简化示例:
https://docs.google.com/document/d/1otjZZlBy6au5E2I7T6NdSYmLayhNGWyXGxGo3gBx_-w/edit