[英]mysql: join two tables with two fields in common to get a third table
I need some help using mysql and phpmyadmin; 我需要使用mysql和phpmyadmin的帮助; I have two tables with two column fields in common, and I need to join both tables into a third where this two fields of each table are in common; 我有两个表,两个表的字段是相同的,我需要将两个表连接到第三个表中,每个表的这两个字段是相同的。 also the reference table is the table 1, so the idea is that 3rd table is actually table 1 with the aditional of table 2 adding value 1 and value 2 from table table 2 into table 1 where Column 1 and Column 2 are the same for both tables... for example: 引用表也是表1,因此想法是第三个表实际上是表1,表2的附加条件是将表2的值1和值2加到表1中,其中第1列和第2列都相同表格...例如:
table1: 表格1:
ID, Column1, Column2, Value1, Value2 ID,Column1,Column2,Value1,Value2
1 , DAN , Citi1 , 1 , 3 1,DAN,花旗1,1,3
2 , JUAN , Citi1 , 5 , 5 2,胡安,花旗1,5,5
3 , DAN , Citi2 , 3 , 7 3,DAN,Citi2,3,7
4 , PEDRO , Citi1 , 2 , 4 4,PEDRO,花旗1,2,4
5 , JUAN , Citi2 , 7 , 9 5,胡安,花旗2,7,9
table2: 表2:
ID, Column1, Column2, Value1, Value2 ID,Column1,Column2,Value1,Value2
1 , DAN , Citi1 , 5 , 0 1,丹,花旗1,5,0
2 , JUAN , Citi1 , 0 , 3 2,胡安,花旗1,0,3
3 , DAN , Citi2 , 4 , 5 3,DAN,花旗2,4,5
4 , JUAN , Citi2 , 6 , 8 4,胡安,花旗2,6,8
table 3, join: 表3,加入:
ID, t1/t2.Column1 , t1/t2.Column2, t1.Value1, t1.Value2, t2.Value1, t2.Value2 ID,t1 / t2.Column1,t1 / t2.Column2,t1.Value1,t1.Value2,t2.Value1,t2.Value2
ID, t1.Column1, t1.Column2, t1.Value1, t1.Value2, t2.Value1, t2.Value2 ID,t1.Column1,t1.Column2,t1.Value1,t1.Value2,t2.Value1,t2.Value2
1 , DAN , Citi1 , 1 , 3 , 5 , 0 1,丹,花旗1,1,3,5,0
2 , JUAN , Citi1 , 5 , 5 , 0 , 3 2,胡安,花旗1,5,5,0,3
3 , DAN , Citi2 , 3 , 7 , 4 , 5 3,DAN,Citi2,3,7,4,5
4 , PEDRO , Citi1 , 2 , 4 , 0 , 0 4,PEDRO,Citi1,2,4,0,0
5 , JUAN , Citi2 , 1 , 3 , 6 , 8 5,胡安,花旗2,1,3,6,8
Try this: 尝试这个:
SELECT t1.ID,
t1.Column1,
t1.Column2,
t1.Value1,
t1.Value2,
COALESCE(t2.Value1, 0),
COALESCE(t2.Value2, 0)
FROM table1 t1
LEFT JOIN table2 t2
ON t1.ID = t2.ID
AND t1.column1 = t2.column1
AND t1.column2 = t2.column2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.