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
Difference between GROUP_CONCAT() and CONCAT_WS()?
提问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_CONCAT
is 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_WS
is 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
回答by cellepo
Pictorial difference:
图片差异:
http://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php
http://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php
GROUP_CONCATbelow
以下GROUP_CONCAT
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php