MySQL GROUP_CONCAT() 和 CONCAT_WS() 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11889391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:27:25  来源:igfitidea点击:

Difference between GROUP_CONCAT() and CONCAT_WS()?

mysql

提问by Matt

I have searched unsuccessfully for a satisfactory explanation on the difference between GROUP_CONCAT()and CONCAT_WS().

我已经搜索不成功上的区别一个满意的解释GROUP_CONCAT()CONCAT_WS()

Are they as closely related as I think they are?

它们是否像我认为的那样密切相关?

What are the differences in usage, speed, etc. between these two functions?

这两个功能在用法、速度等方面有什么区别?

回答by Kalpesh

GROUP_CONCATis used when you want to have non-NULL values from different column rows in a single row. For this you need to have GROUP BY to work.

GROUP_CONCAT当您希望在一行中包含来自不同列行的非 NULL 值时使用。为此,您需要让 GROUP BY 工作。

CONCAT_WSis to join two or more strings.

CONCAT_WS是连接两个或多个字符串。

Example,

例子,

GROUP_CONCAT(CONCAT_WS(' ', firstname, lastname) ORDER BY id ASC SEPARATOR ',');

GROUP_CONCAT(CONCAT_WS(' ', firstname, lastname) ORDER BY id ASC SEPARATOR ',');

Outputs something like,

输出类似,

John Doe,Blah Blah,Tom Cruise,Lorem Ipsum

John Doe,Blah Blah,Tom Cruise,Lorem Ipsum

here space between the name is because of CONCAT_WS, while whole result in one row is because of GROUP_CONCAT

这里名称之间的空格是因为CONCAT_WS,而一行中的整个结果是因为GROUP_CONCAT