我有2张桌子。一个看起来像这样(它包含更多字段,但这不重要):
TABLE 'containers';
INT id;
VARCHAR(20) name;
VARCHAR(20) uid;
TABLE 'sync';
INT id;
VARCHAR(20) containerUid;
VARCHAR(20) deviceUid;
现在我需要获取所有容器,在一个特定的deviceUid的'sync'表中没有条目。我试过这种方式,但结果似乎不正确。
SELECT * FROM (SELECT a.*, (SELECT COUNT(*) FROM sync AS b WHERE a.uid = b.containerUid AND b.deviceUid = 'DEVICEUID') AS anzahlSync FROM containers AS a) AS A WHERE anzahlSync = 0
我想在mysql中这样做,因为containers-table包含很多条目。所以我尽量不让它们回来,但是之前过滤它们。
答案 0 :(得分:0)
使用not exists
:
select *
from containers c
where not exists (select 1 from sync s where s.containerUid = c.id and s.deviceUid = 'DEVICEUID')
答案 1 :(得分:0)
当您想要覆盖其中一个表中的NULL条目时,您可以使用此查询:
SELECT *
FROM
(
SELECT *
FROM containers
WHERE name LIKE 'DEVICEUID'
UNION ALL
SELECT id, containerUid as name, deviceUid as uid
FROM sync
WHERE name LIKE 'DEVICEUID'
) AS diff
GROUP BY name, uid
HAVING COUNT(*) = 1