2013-03-01 158 views
1

我想從其中涉及需要從兩個表進行比較的記錄列表中的where子句中的表列出行要使用SELECT查詢的WHERE子句

SELECT DISTINCT (CONA.DOCUMENTID), CONA.CONTENTID 
FROM TABLEA AS CONA 
INNER JOIN TABLEB AS CTAGA ON CTAGA.CONTENTID = CONA.CONTENTID 
WHERE CONA.DOCUMENTID LIKE 'PM%' 
AND (
    SELECT TAGID 
    FROM TABLEB 
    WHERE CTAGA.CONTENTID = CONA.CONTENTID 
) IN (SELECT TAGID FROM TABLEC) 

在上面SELECT TAGID FROM TABLEB WHERE CTAGA.CONTENTID = CONA.CONTENTID將有

ABC,DEF,GHI,JKL,MNO

SELECT TAGID FROM TABLEC將有ABC,DEF,GHI,JKL,MNO,PQR,STU

是否有可能檢查列表A中的所有記錄是有名單B

在表使用上面的我另外的多重價值

回答

0

試試這個

SELECT DISTINCT (CONA.DOCUMENTID),TAGID FROM TABLEB As CTAGA 
Join TABLEC As CONA On CTAGA.CONTENTID = CONA.CONTENTID 
Where CTGA.TAGID in (SELECT TAGID FROM TABLEC) 
AND CONA.DOCUMENTID LIKE 'PM%' 
0

試試這個查詢

SELECT DISTINCT (CONA.DOCUMENTID), CONA.CONTENTID 
FROM TABLEA AS CONA INNER JOIN TABLEB AS CTAGA ON CTAGA.CONTENTID = CONA.CONTENTID 
         AND CTAGA.TAGID IN (SELECT TAGID FROM TABLEC)      
WHERE CONA.DOCUMENTID LIKE 'PM%' 

OR

SELECT DISTINCT (CONA.DOCUMENTID), CONA.CONTENTID 
FROM TABLEA AS CONA INNER JOIN TABLEB AS CTAGA ON CTAGA.CONTENTID = CONA.CONTENTID           
WHERE CONA.DOCUMENTID LIKE 'PM%' AND EXISTS (
              SELECT 1 
              FROM TABLEC 
              WHERE CTAGA.TAGID = TABLEC.TAGID 
              )