大家好,请帮我为该场景连接一个 sql 查询
表
+----+---------+--------+------------+--------------+
| ID | country | region | restaurant | locationcode |
+----+---------+--------+------------+--------------+
| 1 | IND | DL | xyz | 100 |
| 2 | IND | DL | yzc | 111 |
+----+---------+--------+------------+--------------+
选择所有项目 ---
if condition matched with country,region,restaurant,locationcode
If not then all items that matched condition with country,region,restaurant
if not then all items that matched the condition with country,region
我可以使用 sql 查询来做到这一点,还是必须使用应用程序逻辑来处理它?</p>
最佳答案
这个解决方案有点冗长,但应该可行。使用 CASE
从选项中进行选择,并使用这些行的存在作为选择条件。
SELECT id FROM mytable WHERE
CASE
WHEN EXISTS( SELECT 1 FROM mytable WHERE region='DL' AND country='IND'
AND restaurant='xyz' AND locationcode='100')
THEN region='DL' AND country='IND' AND restaurant='xyz' AND locationcode='100'
WHEN EXISTS( SELECT 1 FROM mytable WHERE region='DL' AND country='IND')
THEN region='DL' AND country='IND'
WHEN EXISTS( SELECT 1 FROM mytable WHERE region='DL' )
THEN region = 'DL';
END;
关于mysql - 这个场景需要一个sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25441133/