1

我想列出与 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 中

使用上面我另外在表中的多个值

4

2 回答 2

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%'

或者

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
                                             )
于 2013-03-01T08:41:48.297 回答
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%'
于 2013-03-01T08:20:17.363 回答