查找零类别的单位

时间:2010-06-01 14:37:54

标签: sql mysql

我正在使用SQL命令查找没有类别的单元。以下是表格:

单元

id  name

UnitCategory

id  name

UnitCategoryIndex

id  categoryid  unitid

谢谢!

3 个答案:

答案 0 :(得分:3)

仔细检查语法(我为SQL Server编写)。

SELECT u.id, u.name
FROM Unit as u
LEFT JOIN UnitCategoryIndex as uci
ON u.id = uci.UnitId
where uci.id is null

答案 1 :(得分:1)

SELECT id FROM Unit WHERE NOT EXISTS (SELECT 1 FROM UnitCategoryIndex WHERE Unit.id = UnitCategoryIndex.unitid)

答案 2 :(得分:1)

select *
  from unit U
 where not exists ( select *
                      from unitcategoryindex X
                     where X.unitid = U.id
                  )