[英]Select all records from one table and referenced values from another table?
2张桌子:
table1 (id, name)
table2 (id, table1_id, unique_identifier)
我想从table1中获取所有行,并确定每个行是否在table2中具有特定的unique_identifier的行。
例:
tabel1:
1, name1
2, name2
3, name3
table2:
1, 2, ident1
2, 3, ident2
结果我想要:
name1
name2, ident1
name3, ident2
您可以使用LEFT JOIN
SELECT table1.name, table2.unique_identifier
FROM table1
LEFT JOIN table2
ON table1.id = table2.table1_id
这样,如果第二张表中没有相应的记录,则唯一标识符列将为null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.