gpt4 book ai didi

sql - 从一个表中选择所有记录并从另一个表中选择引用值?

转载 作者:行者123 更新时间:2023-11-29 12:56:47 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com