3

我在mysqlgroup concats记录中有一个查询,并给出了一个具有冗余标记的值。以下是查询的输出:

Problem Area->ACC-HO->ACC-HO->Credit Note (C/N)->Problem description ->Problem description 

但我想要这个字符串的不同标记如下

Problem Area->ACC-HO->Credit Note (C/N)->Problem description 

有没有办法在 sqlSELECT查询本身中做到这一点?

编辑 这是架构和查询 以下是我的查询:

SELECT 
t2.transaction_id AS transaction_id, 
GROUP_CONCAT(
CONCAT(
 t1.display_text,
 '->',
   (CASE (NOT EXISTS (SELECT 1 FROM mst_node a WHERE a.parent_node_id = t1.node_id))
      WHEN 1 THEN t1.display_text ELSE 
      (SELECT b.display_text AS DISPLAY FROM mst_node b 
      WHERE parent_node_id = t2.node_id AND b.display_seq = t2.entered_value) 
     END)
     ) 
  ORDER BY t2.logtime_stamp SEPARATOR '->'
) AS display_text 
FROM

mst_node t1 
  JOIN trn_user_log t2 
    ON t1.app_id = t2.app_id AND t1.node_id = t2.node_id     
WHERE (t1.app_id = 105) 
  AND t1.parent_node_id IS NOT NULL 
  AND t1.save_as_default IS NULL 
GROUP BY transaction_id,
  mobile_no 
ORDER BY t2.transaction_id DESC,
  t2.logtime_stamp,
  t2.mobile_no 
4

3 回答 3

5

在您GROUP CONCAT添加DISTINCT时,它只会连接唯一值。

SELECT GROUP_CONCAT(DISTINCT colName),....
FROM   ...
WHERE  ...
GROUP BY ...

SQLFiddle 演示

于 2012-09-26T08:52:51.577 回答
0

在您的分组子句中,您也可以group by按此字符串列添加另一个分组。

SELECT GROUP_CONCAT(myStringColumn),....
FROM   ...
WHERE  ...
GROUP BY myOtherColumn,myStringColumn
于 2012-09-26T08:54:08.077 回答
0

我终于能够通过使用UNION两个选择查询中的一个然后执行GROUP_CONCAT(DISTINCT column ORDER BY another_column).

以下是我使用的查询:

SELECT 
transaction_id,
GROUP_CONCAT(DISTINCT display_text ORDER BY logtime_stamp SEPARATOR '->') AS display_text
FROM
 (SELECT 
  t2.transaction_id AS transaction_id,
  t2.logtime_stamp,
  t1.display_text AS display_text 
  FROM mst_node t1 
  JOIN trn_user_log t2 ON t1.app_id = t2.app_id 
   AND t1.node_id = t2.node_id 
   WHERE (t1.app_id = 105) 
   AND t1.parent_node_id IS NOT NULL 
   AND t1.save_as_default IS NULL 
  UNION
 SELECT t2.transaction_id AS transaction_id,t2.logtime_stamp,
  CASE(NOT EXISTS (SELECT 1 FROM mst_node a WHERE a.parent_node_id = t1.node_id)) 
  WHEN 1 THEN NULL 
  ELSE (SELECT b.display_text AS display_text FROM mst_node b WHERE parent_node_id = t2.node_id AND b.display_seq = t2.entered_value) 
  END AS display_text
FROM   mst_node t1 JOIN trn_user_log t2 
      ON t1.app_id = t2.app_id AND t1.node_id = t2.node_id    
  WHERE (t1.app_id = 105) 
  AND t1.parent_node_id IS NOT NULL 
  AND t1.save_as_default IS NULL 
  ORDER BY transaction_id DESC,logtime_stamp
  ) AS T 
  GROUP BY transaction_id 
  ORDER BY transaction_id DESC,logtime_stamp 
于 2012-09-26T13:20:01.490 回答