gpt4 book ai didi

mysql - 如何合并两个表并连接两个值

转载 作者:行者123 更新时间:2023-11-29 07:07:37 25 4
gpt4 key购买 nike

我有这两张表

tbl_Output

ref_id1 | desc1 | rec_type
1 value1 1
1 value2 2



tbl_Output2

ref_id2 | desc2 | rec_type
1 value3 1
1 value4 2

如何连接这两个表?显示

ref_id1 | desc1  | ref_id2 | desc2  | rec_type
1 value1 1 value3 1
1 value2 1 value4 2

而不是

ref_id1 | desc1  | ref_id2 | desc2  | rec_type
1 value1 1 value3 1
1 value2 1 value4 2
1 value1 1 value3 1
1 value2 1 value4 2

Here is my Query:
SELECT *
FROM tbl_Output1 as O
inner JOIN tbl_Output2 as O2 on
O.ref_id1 = O2.ref_id2

注意:我已经使用了不同类型的连接。

最佳答案

尝试加入id1/id2rec_type栏目:

SELECT t1.ref_id1,
t1.desc1,
t2.ref_id2,
t2.desc2,
t1.rec_type
FROM tbl_Output t1
INNER JOIN tbl_Output2 t2
ON t1.ref_id1 = t2.ref_id2 AND
t1.rec_type = t2.rec_type

此处演示:

SQLFiddle

关于mysql - 如何合并两个表并连接两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41051663/

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