2017-02-04 35 views
0

所以,我有表,他们都有一个“entity_id”,但其中一个有一个额外的列名为“价格”,另一个表有两个额外的称为“邮政编码”和“城市”。SQL DB与两个不同的表结合

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代码来做到这一点?

回答

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); 
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;