Sql 如何组合两个表以获得所需的结果
我有两张桌子,设备和产品。设备表包含列id和设备。 和product表有id列和product列 设备表为Sql 如何组合两个表以获得所需的结果,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有两张桌子,设备和产品。设备表包含列id和设备。 和product表有id列和product列 设备表为 id device 1 a 2 b 3 b id product 1 x 2 y 3 z 4 s 产品表为 id device
id device
1 a
2 b
3 b
id product
1 x
2 y
3 z
4 s
产品表为
id device
1 a
2 b
3 b
id product
1 x
2 y
3 z
4 s
我需要结果
id device
1 a
2 b
3 b
4 null
试试这个
select p.id,d.device from products p outer join device d
on d.id=p.id
像这样的怎么样
SELECT p.id,
d.device
FROM product p LEFT JOIN
device d ON p.id = d.id
另外,请查看您应该尝试此加入
select p.id,d.Device
from tblDevice d
right join tblProduct p
on d.id=p.id