1

主キー「poll_id」と「object_id」という名前の列を持つ2つのテーブルがあります。t2に存在せず、poll_id(主キー)を使用しているt1からのオブジェクトの取得を管理できません。

       T1                           T2

poll_id|object_id            poll_id|object_id
1      | 2                    1     |  2
1      | 3                          |

私は取得する必要があります[poll_id]=>1,[object_id]=>3

4

2 に答える 2

2

試す

select t1.* from t1 
left join t2 on t1.poll_id = t2.poll_id and t1.object_id = t2.object_id
where t2.object_id is null
于 2012-08-09T15:02:30.120 に答える
2

NOTEXISTSを使用する

SELECT poll_id, object_id
FROM t1
WHERE NOT EXISTS
      ( SELECT *
        FROM t2
        WHERE t2.poll_id = t1.poll_id
          AND t2.object_id = t1.object_id
      ) 
  AND poll_id = 1 ;                        -- additional requirement

LEFT JOIN /ISNULLを使用する

SELECT t1.poll_id, t1.object_id
FROM t1
  LEFT JOIN t2
    ON  t2.poll_id = t1.poll_id
    AND t2.object_id = t1.object_id
WHERE t2.object_id IS NULL 
  AND t1.poll_id = 1 ;                        -- additional requirement

NOT INを使用する:

SELECT poll_id, object_id
FROM t1
WHERE (poll_id, object_id) NOT IN
      ( SELECT poll_id, object_id
        FROM t2
      ) 
  AND poll_id = 1 ;                        -- additional requirement
于 2012-08-09T15:07:33.337 に答える