本文介紹了如何使用 GROUP BY 在 MySQL 中連接字符串?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
基本上問題是如何從中獲得:
<前>foo_id foo_name1 安1 乙2 C為此:
<前>foo_id foo_name1 甲乙2 C 解決方案
SELECT id, GROUP_CONCAT(name SEPARATOR ' ') FROM table GROUP BY id;
https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
從上面的鏈接,GROUP_CONCAT
:此函數返回一個字符串結果,其中包含來自組的連接的非 NULL 值.如果沒有非 NULL 值,則返回 NULL.
Basically the question is how to get from this:
foo_id foo_name 1 A 1 B 2 C
to this:
foo_id foo_name 1 A B 2 C
解決方案
SELECT id, GROUP_CONCAT(name SEPARATOR ' ') FROM table GROUP BY id;
https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
From the link above, GROUP_CONCAT
: This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values.
這篇關于如何使用 GROUP BY 在 MySQL 中連接字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!