[英]SQl query Multiple select statments in the same table under different conditions
[英]SQL Query Multiple conditions on a column (On same Table)
使用存在和不存在
select camid from table_name t1
where not exists ( select 1 from table_name t2 where t1.camid=t2.camid
and Specs= 'Spec 5')
and exists ( select 1 from table_name t2 where t1.camid=t2.camid
and Specs= 'Spec 2')
试试这个下面的脚本 -
SELECT DISTINCT Cam_ID
FROM your_table
WHERE Cam_ID NOT IN
(
SELECT DISTINCT Cam_ID
FROM your_table
WHERE Specs = 'Specs 5'
)
AND Specs = 'Specs 2'
AND Cam_ID IS NOT NULL
SELECT Cam_ID
FROM source_table
WHERE Cam_ID IS NOT NULL -- if Cam_ID cannot be zero then simply
-- WHERE Cam_ID
GROUP BY Cam_ID
HAVING SUM(Specs = 'Specs 2') -- at least one 'Specs 2'
AND !SUM(Specs = 'Specs 5') -- and none 'Specs 5'
下面的 SQL 脚本应该为您提供所需的输出。
SELECT CamId FROM (
SELECT t.CamId,
STUFF((
SELECT ',' + Specs FROM
(SELECT t1.Specs FROM test_table as t1
WHERE t1.CamId = t.CamId) AS T
FOR XML PATH('')
),1,1,'') AS specs
FROM test_table AS t
WHERE CamId IS NOT NULL
GROUP BY CamId) AS t5
where t5.specs LIKE '%Specs 2%'
AND t5.specs NOT LIKE '%Specs 5%'
SELECT Cam_Id
FROM table_one t1
WHERE
NOT EXISTS ( SELECT 1 FROM table_one t2 WHERE t1.Cam_Id = t2.Cam_Id AND Specs = 'Spec 5' )
AND EXISTS ( SELECT 1 FROM table_one t2 WHERE t1.Cam_Id = t2.Cam_Id AND Specs = 'Spec 2' )
GROUP BY Cam_Id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.