组合多行消除重复并连接多列

时间:2014-11-04 10:18:40

标签: mysql concatenation distinct concat group-concat

我正在使用mysql并希望执行以下操作:

  1. 对每个列进行区分,并在记录存在多个不同的情况下进行连接
  2.      user_id  pagetitle     companyname   Category1                         Category2               Category3
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Audio Visual Equipment  Audio Visual Equipment- Rentals
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Audio Visual Equipment  Audio Visual Equipment- Rentals
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Audio Visual Equipment  Audio Visual Equipment- Rentals
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Audio Visual Equipment  Audio Visual Equipment- Rentals
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Outdoor Media           Advertising- Signs, Neon, Glow Etc
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Outdoor Media           Advertising- Signs, Neon, Glow Etc
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Printing & Printers     Digital Printing 
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Printing & Printers     Digital Printing 
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Printing & Printers     Digital Printing 
         509      Digital Arts  Digital Arts  Advertising, Media & Promotions   Printing & Printers     Flexographic Printing 
    
         user_id  pagetitle     companyname  Category1                       Category2                                                                        Category3
         509      Digital Arts  Digital Arts    Advertising, Media & Promotions Audio Visual Equipment, Outdoor Media, Printing & Printers, Printing & Printers  Audio Visual Equipment- Rentals, Advertising- Signs, Neon, Glow Etc, Digital Printing , Flexographic Printing 
    

    1. 并且还将多列中的内容合并为1,从而消除空列。

      地址buildingname landmark地点sublocality City State Country密码 5 Vaniga Valaagam旧巴士站Salem Tamilnadu印度636001

    2. 好像我对分隔符使用concat(),上面也包含空并包含多个分隔符。

      5, Vaniga Valaagam, , Old Bus Stand, , Salem, Tamilnadu, India - 636001
      

      有人可以提出一种方法来实现这两种目标。

1 个答案:

答案 0 :(得分:0)

第1部分:

SELECT user_id,
   GROUP_CONCAT(DISTINCT pagetitle SEPARATOR ', ' ) as pagetitle,
   GROUP_CONCAT(DISTINCT companyname SEPARATOR ', ' ) as companyname,
   GROUP_CONCAT(DISTINCT Category1 SEPARATOR ', ' ) as Category1,
   GROUP_CONCAT(DISTINCT Category2 SEPARATOR ', ' ) as Category2,
   GROUP_CONCAT(DISTINCT Category3 SEPARATOR ', ' ) as Category3
FROM some_table
GROUP BY user_id

第2部分:

连接多个字段,省略空字段

SELECT concat(
  IF(field1>'',concat(field1,', '),''),
  IF(field2>'',concat(field2,', '),''),
  IF(field3>'',concat(field3,', '),'')
  ) as something
FROM somewhere

把它放在一起:

SELECT concat(
  IF(user_id>'',concat(user_id,', '),''),
  IF(pagetitle>'',concat(pagetitle,', '),''),
  IF(Category1>'',concat(Category1,', '),''),
  IF(Category2>'',concat(Category2,', '),''),
  IF(Category3>'',concat(Category3,', '),'')
  ) as something
FROM
(
SELECT user_id,
   GROUP_CONCAT(DISTINCT pagetitle SEPARATOR ', ' ) as pagetitle,
   GROUP_CONCAT(DISTINCT companyname SEPARATOR ', ' ) as companyname,
   GROUP_CONCAT(DISTINCT Category1 SEPARATOR ', ' ) as Category1,
   GROUP_CONCAT(DISTINCT Category2 SEPARATOR ', ' ) as Category2,
   GROUP_CONCAT(DISTINCT Category3 SEPARATOR ', ' ) as Category3
FROM some_table
GROUP BY user_id
) nicehomework