2 个表:
table1 (id, name)
table2 (id, table1_id, unique_identifier)
我想获取表 1 中的所有行,并针对每一行确定表 2 中是否有具有特定 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
关于sql - 从一个表中选择所有记录并从另一个表中选择引用值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41212621/