選擇單行多行我有一個接觸表如下:在連接表
contactid | contactname
----------|-------------
C1 | Name1
C2 | Name2
我有一個溝通表如下
contactid | communication_type | communication_string
----------|--------------------|---------------------
C1 | Phone | 9090909090
C1 | Email | [email protected]
C2 | Phone | 9191919191
C2 | Email | [email protected]
現在,我的要求是查詢這兩個表,使得結果如下:
contactid | contactname | phonenumber | emailaddress
----------|-------------|---------------|----------------
C1 | Name1 | 9090909090 | [email protected]
C2 | Name2 | 9191919191 | [email protected]
如果我做了常規連接,如
SELECT cont.contactid, cont.contactname,
comm.communication_type, comm.communication_string
FROM contact cont
LEFT JOIN communication comm ON cont.contactid = comm.contactid
我會像
contactid | contactname | communication_type| communication_string
----------|-------------|-------------------|----------------
C1 | Name1 | Phone | 9090909090
C1 | Name1 | Email | [email protected]
C2 | Name2 | Phone | 9191919191
C2 | Name2 | Email | [email protected]
但這不是我想要的。 我希望結果中的同一行中的通信字符串,而不是不同的行。
這是否可能得到這樣的結果。
還有一個要求是解決方案應該是通用的,可以在所有數據庫上工作。
不要標記每個數據庫傢伙。現在來吧。這是一個關鍵問題,他們都以不同的方式做。 –