剛使用羣組concat with group by如下 - 您的主表 -
| id | sub1 | sub2 | sub3 |
| 1 | kk nagar | Alwartirunagar | | | 1 | Valasaravakkam | Nandambakkam | Ramapuram | | 1 | Porur | Meenambakkam | Ashok Nagar | | 1 |安娜納加| Kodambakkam | Nungambakkam | | 2 | West Mambalam | Virugambakkam | T Nagar | | 2 | KK Nagar | | |
現在當你運行一個查詢 - 從t1 group by id選擇id,group_concat(sub1,'|',sub2) 它會給你如下數據
| id | group_concat(sub1,',',sub2,',',sub3)| | 1 | kk nagar,Alwartirunagar,Valasaravakkam,Nandambakkam,Ramapuram,Porur,Meenambakkam,Ashok Nagar,Anna Nagar,Kodambakkam,Nungambakkam | | 2 | West Mambalam,Virugambakkam,T Nagar,KK Nagar ,, |
現在只需通過以下查詢插入另一個表中的數據 從t1 group by id插入到t2 select select id,group_concat(sub1,',',sub2,',',sub3);
現在你會得到像下面t2的數據
| id | grouped_sub |
| 1 | kk nagar,Alwartirunagar,Valasaravakkam,Nandambakkam,Ramapuram,Porur,Meenambakkam,Ashok Nagar,Anna Nagar,Kodambakkam,Nungambakkam |
| 2 | West Mambalam,Virugambakkam,T Nagar,KK Nagar ,,
希望這會對你有幫助。
注意: - 由於問題要求不同 - 查詢 - 插入到t2選擇* from(從t1選擇id,sub1作爲主題union select id,sub2作爲subject從t1 union select id,sub3作爲來自t1的主題) temp where subject!=''按ID排序;
| id | grouped_sub | + ------ + ---------------- + | 1 | Nungambakkam | | 1 | Alwartirunagar | | 1 | Ramapuram | | 1 | Porur | | 1 | Kodambakkam | | 1 | kk nagar | | 1 | Nandambakkam | | 1 | Ashok Nagar | | 1 |安娜納加| | 1 | Valasaravakkam | | 1 | Meenambakkam | | 2 | West Mambalam | | 2 | T Nagar | | 2 | KK Nagar | | 2 | Virugambakkam |
'group_concat'? – Wanderer
你可以在這個更具體嗎? –
正常化這些數據或將成爲一場噩夢。請參閱[Junction Tables](http:// stackoverflow。com/a/32620163) – Drew