Mysql 如何组合成两个表,并连接两个值
我有这两张桌子Mysql 如何组合成两个表,并连接两个值,mysql,join,Mysql,Join,我有这两张桌子 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
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
/id2
列和rec\u 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
此处演示:
尝试连接id1
/id2
列和rec\u 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
此处演示:
这可能是最简单的方法:
SELECT o1.ref_id1,o1.desc1,o2.ref_id2,o2.desc2,rec_type FROM tbl_Output1 o1
INNER JOIN tbl_Output2 o2 USING(rec_type);
以下是最简单的方法。这可能是最简单的方法:
SELECT o1.ref_id1,o1.desc1,o2.ref_id2,o2.desc2,rec_type FROM tbl_Output1 o1
INNER JOIN tbl_Output2 o2 USING(rec_type);
以下是。您能重新格式化顶部零件吗?
rec\u类型必须匹配吗?是的,应该匹配。我可以使用@tim的建议加入它。你能重新格式化你的顶部吗?rec\u类型必须匹配吗?是的,它应该匹配。我通过@tim的建议加入了它。