SQL DB与两个不同的表组合在一起

时间:2017-02-04 14:17:13

标签: mysql

所以,我有表,他们都有一个“entity_id”,但其中一个有一个名为“price”的额外列,另一个表有两个额外的名为“postcode”和“city”。

Like this:          The other:
_________________   ___________________________
|entity_id|price|   |entity_id|postcode|city  |  
|1        |23$  |   |1        |12345   |some1 |
|2        |10$  |   |2        |54321   |some2 |

我想要的是:

__________________________________
|entity_id|price|postcode|city   |
|1        |23$  |12345   |some1  |
|2        |10$  |54321   |some2  |

但我找不到任何SQL代码来执行此操作?

2 个答案:

答案 0 :(得分:0)

这是简单的加入:

select
    a.entity_id,
    a.price,
    b.postcode,
    b.city
from table1 a
join table2 b
on a.entity_id = b.entity_id;

或者简单地说:

Select *
from table1 a
join table2 b
using (entity_id);

答案 1 :(得分:0)

Usng加入 http://www.w3schools.com/sql/sql_join_inner.asp

示例:

 SELECT table_name1.entity_id, table_name1.price, table_name2.postcode, table_name2.city 
    FROM table_name1
    INNER JOIN table_name2
    ON table_name1.entity_id=table_name2.entity_id;