2017-02-04 43 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;