应用在相关数据中出现多个值的条件

时间:2019-03-17 11:50:16

标签: sql

我有这张桌子TCM

enter image description here

我想从Topic_hd_id中选择不同的TCM,其中TCM.VALUE来自另一个临时表T

例如,临时表T的值为1000,4

SELECT DISTINCT(TOPIC_HD_ID) 
FROM TRN.TOPICS_CATEGORY_MAPPING AS TCM 
WHERE TCM.VALUE IN(1000,4)

它显示:

enter image description here

但是我需要申请WHERE VALUE=1000 AND VALUE=4

使用TPIC_HD_ID = 255时结果只能是一行,但是使用IN时结果将显示3行。

2 个答案:

答案 0 :(得分:2)

使用条件聚合:

Error starting at line : 2 in command -
update invoice 
set invoice_status_code = 1
where invoice_number = 2
Error report -
ORA-20005: Cannot update Orders because Shipment exists.

答案 1 :(得分:1)

额外的HAVING子句:

SELECT TOPIC_HD_ID
FROM TRN.TOPICS_CATEGORY_MAPPING AS TCM 
WHERE TCM.VALUE IN(1000,4)
GROUP BY TOPIC_HD_ID
HAVING COUNT(DISTINCT VALUE) = 2

另外,也可以使用INTERSECT

SELECT TOPIC_HD_ID
FROM TRN.TOPICS_CATEGORY_MAPPING AS TCM 
WHERE TCM.VALUE IN(1000)
INTERSECT
SELECT TOPIC_HD_ID 
FROM TRN.TOPICS_CATEGORY_MAPPING AS TCM 
WHERE TCM.VALUE IN(4)