我的表格如下:
ID | Advert_id | Iso_code
----|------------|-----------
1 | 22 | en_US
2 | 22 | de_DE
3 | 11 | zh_CE
4 | 11 | nl_NL
我需要返回ID
,例如我有Advert_ID = 22
ID
1和2 iso_code = en_US
和de_DE
但Advert_ID
有iso_code = en_US
id = 1
仅返回Advert_ID = 11
如果不返回所有ID(iso_code = en_US
没有Id
,那么返回display: inline-block
3和4)
答案 0 :(得分:0)
使用or not exists()
select *
from t
where (t.iso_code = 'en_US'
or not exists (
select 1
from t as i
where t.Advert_id = i.Advert_id
and i.iso_code = 'en_US')
)
测试设置:http://rextester.com/SGH43248
返回:
+----+-----------+----------+
| id | advert_id | iso_code |
+----+-----------+----------+
| 1 | 22 | en_US |
| 3 | 11 | zh_CE |
| 4 | 11 | nl_NL |
+----+-----------+----------+
答案 1 :(得分:0)
来自你的评论:
我需要选择所有" Advarte_ID"并检查iso_code if =' en_US"只返回带有' en_US'
如果我理解你,那将有所帮助:
SELECT ID
FROM YourTablName
WHERE iso_code = 'en_US';
这将仅返回Advert_ID
s,其中包含`iso_code =' en_US'。
<强>更新强>
IF EXISTS (SELECT * FROM YourTable WHERE iso_code = 'en_US')
BEGIN
SELECT ID
FROM YourTablName
WHERE iso_code = 'en_US';
END
ELSE
BEGIN
SELECT ID
FROM YourTable;
END