mysql group_concat

SELECT group_concat(town) FROM `players` group by town;
//group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果。
//group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来。要返回哪些列,由函数参数(就是字段名)决定。分组必须有个标准,就是根据group by指定的列进行分组。
//CREATE TABLE `crawl_data` (
    `url` varchar(255) NOT NULL,
    `code` varchar(255) NOT NULL,
    `name` varchar(255) DEFAULT NULL,
    `category` varchar(255) DEFAULT NULL,
    `order_index` int(255) DEFAULT NULL,
    `fetch_time` datetime NOT NULL,
    `text_value` text,
    `html_value` text,
    `date_value` datetime DEFAULT NULL,
    `num_value` decimal(18,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SELECT url ,fetch_time, 
    GROUP_CONCAT(CASE WHEN  code = 'domain'  THEN  text_value ELSE  null  END)   AS  `domain`,
    GROUP_CONCAT(CASE WHEN  code = 'name'  THEN  text_value ELSE  null  END)   AS  `name`,
    GROUP_CONCAT(CASE WHEN  code = 'brand'  THEN  text_value ELSE  null  END)   AS  `brand`,
    GROUP_CONCAT(CASE WHEN  code = 'category'  THEN  text_value ELSE  null  END)   AS  `category`,
    GROUP_CONCAT(CASE WHEN  code = 'purpose'  THEN  text_value ELSE  null  END)   AS  `purpose`,
    GROUP_CONCAT(CASE WHEN  code = 'price'  THEN  num_value ELSE  null  END)   AS  `price`,
    GROUP_CONCAT(CASE WHEN  code = 'refPrice'  THEN  num_value ELSE  null  END)   AS  `refPrice`,
    GROUP_CONCAT(CASE WHEN  code = 'primaryImage'  THEN  text_value ELSE  null  END)   AS  `primaryImage`
    FROM crawl_data GROUP BY url,fetch_time

 

posted @ 2017-11-21 14:54  雨落知音  阅读(1143)  评论(0编辑  收藏  举报