SQL过滤查询;如何为一个国家/地区(法国)提供某些内容,但不为其他国家/地区(某些欧洲国家)提供内容

时间:2019-01-28 17:20:14

标签: mysql sql

说在一个名为country_data的表中,我有5个国家及其一些行业

国家/地区字段: 葡萄牙, 法国, 意大利, 德国, 西班牙

行业字段: 葡萄酒, 起司, 啤酒

但是我只想为法国添加“奶酪”。在仍将其包括在法国的情况下,如何过滤掉其他国家/地区?

我正在尝试使用CASE,但是它不能按计划工作

谢谢

SELECT country, 
       industry
FROM country_data
WHERE (

    industry = 'Wine' OR
    industry = 'Beer' OR
    CASE WHEN country = 'France' THEN industry = 'Cheese' ELSE null 
    END

)

预期结果将是所有国家的葡萄酒和啤酒,但只有法国的奶酪

2 个答案:

答案 0 :(得分:1)

有2种不同的条件,必须将它们与OR结合使用:

SELECT 
  country, 
  industry
FROM country_data
WHERE 
  country = 'France' 
  OR
  (country <> 'France' AND industry <> 'Cheese') 

或等价物:

SELECT 
  country, 
  industry
FROM country_data
WHERE 
  country = 'France' OR industry <> 'Cheese' 

答案 1 :(得分:-1)

Barmar帮助了我这个答案。

SELECT country, 
       industry
FROM country_data
WHERE (

    industry = 'Wine' OR
    industry = 'Beer' OR
    (country = 'France' and industry = 'Cheese')

)
相关问题