MySQL绑定两个具有不同维度的表

时间:2017-03-28 08:48:36

标签: mysql join union

我有两张桌子

表A:

X_Origin  | date    | uploads |    
--------------------------------
1         | 201501  | 10       |    
2         | 201501  | 50       | 
3         | 201502  | 70       | 
4         | 201503  | 150      | 
5         | 201503  | 120      |

表B:

X_Origin  | X_COUNTRY_CODE  | X_COUNTRY  |    
--------------------------------
1         | 34              | SPAIN      |    
2         | 21              | GERMANY    | 
4         | 34              | SPAIN      | 
5         | 21              | GERMANY    |

我需要按日期分组,我可以想象西班牙的信息(总结上传)。我需要这样的东西:

X_Origin  | date    | uploads |  X_COUNTRY  |   
---------------------------------------
1         | 201501  | 10      |   Spain 
4         | 201503  | 150     |   Spain 

1 个答案:

答案 0 :(得分:0)

请试试这个 -

SELECT A.X_Origin, A.date, A.uploads, B.X_COUNTRY
FROM A INNER JOIN B ON A.A.X_Origin = B.A.X_Origin
WHERE B.X_COUNTRY = 'Spain'
AND A.X_dest = 'Spain'