2017-10-19 53 views
0

我有兩個表:SQLite的:查詢列出

表1:

ID_Number | Description| 
a   | abc 
b   | cde 
c   | fgh 

表2:

ID | Title   | value 
a | phone number | 555-1234 
a | address  | somewhere USA 
a | mobile_number | 867-5309 
b | phone number | ... 
b | address  | ... 
b | mobile_number | ... 

我想創建一個新表屬於每個的所有數據存在於表2中的ID_number。

查詢我可以列出所有。

select Table1.ID_Number, Table2.ID from Table1, Table2 
where Table1_ID_number = Table2.ID)) 
order by ID_Number 

但我想創建一個新表:

Table3 
    |ID | Description| ID_data 
    a , abc, "Address_A_PhoneNumber_A_Mobile_Number_A" 
    b , cde, "Address_B_PhoneNumber_B_Mobile_Number_B" 

這可能與SQL ???

回答

1

我需要編輯您的問題,並將第三列添加到包含各種類型的信息值的第二個表。沒有這樣的事情,我怕你的問題沒有道理。

我們可以按ID進行彙總,並將地址和電話號碼轉換爲單個連接字符串。

SELECT 
    t1.ID_Number, 
    t1.Description 
    MAX(CASE WHEN t2.title = 'address'  THEN t2.value END) || '_' || 
    MAX(CASE WHEN t2.title = 'phone number' THEN t2.value END) || '_' || 
    MAX(CASE WHEN t2.title = 'mobile_number' THEN t2.value END) AS ID_data 
FROM Table1 t1 
INNER JOIN Table2 t2 
    ON t1.ID_Number = t2.ID 
GROUP BY 
    t1.ID_Number, 
    t1.Description;