我有一个会返回值的查询,但我需要它们作为由逗号分隔的单个输出.
所以我试着用逗号连接输出,但它不起作用?
select id from videos where duration=0; /// this would return some rows
Run Code Online (Sandbox Code Playgroud)
我试过concat和concat_ws但是没有用
select concat(select concat(id,',') from videos where duration=0);
select concat((select id from videos where duration=0),',');
select concat_ws(',',(select id from videos where duration=0));
Run Code Online (Sandbox Code Playgroud)
我需要逗号separtor所有行的id
例如,输出应为1,4,6,78,565
有任何想法吗?
Mar*_*ith 40
这就是group_concat的作用.
select group_concat(id) as video_list
from videos
where duration=0
Run Code Online (Sandbox Code Playgroud)
尝试使用 GROUP_CONCAT
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
Run Code Online (Sandbox Code Playgroud)
参考:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
使用group_concat:
此函数返回字符串结果,其中包含来自组的连接非NULL值.如果没有非NULL值,则返回NULL.
SELECT
GROUP_CONCAT(id)
FROM
videos
WHERE
duration=0
Run Code Online (Sandbox Code Playgroud)