gpt4 book ai didi

mysql - 连接 SQL 表而不更改第一个表

转载 作者:行者123 更新时间:2023-11-29 10:49:08 26 4
gpt4 key购买 nike

我正在尝试使用下面的代码将两个表连接在一起,但我希望“tab1”中的行保持不变。

create table tab3 select * from tab1 left join tab2 on tab1.`tab1`=tab2.`tab2`;

Tab1
a
b
c
d
e
f
g


Tab2
a
a
c
d

但是我得到了这个结果,Tab1 中的“a”加倍了

Tab1    Tab2
a a
a a
b b
c c
d null
e null
f null
g null

我需要这个结果,而不是 Tab1 行保持不变,如下表

Tab1    Tab2
a a
b b
c c
d null
e null
f null
g null

最佳答案

它的效率不是很高,但你可以使用select unique:

select distinct *
from tab1 left join
tab2
on tab1.`tab1` = tab2.`tab2`;

或者仅在 tab2 本身上:

select distinct *
from tab1 left join
(select distinct tab2.* from tab2) tab2
on tab1.`tab1` = tab2.`tab2`;

关于mysql - 连接 SQL 表而不更改第一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44060546/

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