用于选择没有黑色家具的房屋的SQL查询?

时间:2018-01-10 11:32:06

标签: sql sql-server database

我在SQL数据库中有3个表:

house(id,Name), 
furniture(id,Name,Color) 
house_furniture(id,HouseID,FurnitureID).

如何在不使用子查询的情况下构建查询以获取没有黑名表的所有房屋名称?

2 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情,这是sql-server,因为你没有标记任何东西

select h.Name
from house_furniture hf 
join house h on hf.HouseId = h.id
left join furniture f on hf.FurnitureID = f.id 
                     and f.Color = 'Black'
                     and f.Name = 'Table'
where f.Id IS NULL

答案 1 :(得分:0)

此查询适用于MySQL 5.7 -

SELECT h.NAME
FROM house h
    ,furniture f
    ,house_furniture hf
WHERE hf.HouseID = h.id
    AND hf.FurnitureID = f.id
    AND f.NAME = 'tables'
    AND f.colour != 'black ;'