1

我有两个通过 MAP 表组合的表

Table ANIMAL:
+------+--------------+
| id   |  description |
+------+--------------+
|  2   |  Ape         |
|  3   |  Lion        |
+------+--------------+


Table MAP:
+-----------+---------+
| animal_id | legs_id |
+-----------+---------+
|   2       | 11      |
+-----------+---------+


Table LEGS:
+------+--------------+
|  id  |     legs     |
+------+--------------+
| 10   |      4       |
| 11   |      2       |
+------+--------------+

我需要在 LEGS 表中没有地图条目的动物,如下所示:

 !(select *  
  from ANIMAL as a  
  JOIN MAP as m ON (a.id = m.animal_id)  
  JOIN LEGS as l ON (m.legs_id = l.id) )

结果应该给我“狮子”

4

4 回答 4

6

利用LEFT JOIN

SELECT  a.*
FROM    animal a
        LEFT JOIN Map b
            On a.id = b.animal_id
WHERE   b.animal_id IS NULL

要进一步了解有关联接的更多信息,请访问以下链接:

于 2013-02-11T08:20:36.523 回答
4
Select * from Animal A
left join Map M on A.id=M.animal_id
where M.animal_id is null;
于 2013-02-11T08:21:01.493 回答
4

尝试这个 :

SELECT a.*
FROM animal a
WHERE a.id NOT IN (SELECT animal_id FROM Map m JOIN Legs l
                   ON m.legs_id = l.id)
于 2013-02-11T08:21:50.357 回答
0

Could you not do a simple query to return all animals with no associated Map record... i.e.

SELECT
   *
FROM
   Animal
WHERE
   animal_id not in (SELECT animal_id FROM Map)
于 2013-02-11T08:22:47.320 回答