查询时MYSQL Select Case

时间:2014-08-06 17:14:50

标签: mysql

我正在尝试执行下面的mysql查询,我无法看到我做错了什么,我期望从查询的WHEN部分得到结果,而我从ELSE PART得到结果。

SELECT 
   CASE 
   WHEN `club_name` = '7' THEN (
      SELECT group_concat(DISTINCT mobile) from member WHERE `club_name` = '7'
   ) 
   ELSE (SELECT group_concat(mobile) from member) 
   END AS mobile 
FROM member

提前致谢。

1 个答案:

答案 0 :(得分:0)

我认为这是mysqls extensions to the group by statement烧伤的案例:

select list可以引用GROUP BY子句中未命名的非聚合列

...服务器可以自由选择每个组中的任何值,因此除非它们相同,否则所选的值是不确定的。 ......

SELECT 
   CASE 
   WHEN `club_name` = '7' THEN (
      SELECT group_concat(DISTINCT mobile) from member WHERE `club_name` = '7'
   ) 
   ELSE (SELECT group_concat(mobile) from member) 
   END AS mobile 
FROM member
GROUP BY mobile

SQL Fiddle

WITH GROUP BY返回(我相信你的追随者)

MOBILE
abc,def,def,xyz,xyz,xyz,lmn
abc,def,def,xyz,xyz,xyz,lmn
xyz,lmn
abc,def,def,xyz,xyz,xyz,lmn

没有分组:

MOBILE
abc,def,def,xyz,xyz,xyz,lmn
abc,def,def,xyz,xyz,xyz,lmn
abc,def,def,xyz,xyz,xyz,lmn
abc,def,def,xyz,xyz,xyz,lmn
xyz,lmn
xyz,lmn
xyz,lmn

只返回1行...

SELECT 
   CASE 
   WHEN `club_name` = '7' THEN (
      SELECT group_concat(DISTINCT mobile) from member WHERE `club_name` = '7'
   ) 
   ELSE (SELECT group_concat(mobile) from member) 
   END AS mobile 
FROM member
WHERE `club_name` = '7'
GROUP BY mobile