在group_concat mysql里面排序

时间:2014-10-20 13:56:05

标签: mysql sql group-concat

我有一个下面的mysql查询。它返回了错误的价值。请帮助我解决这个问题。

SELECT
    T1.PARENT_ID,
    GROUP_CONCAT(
        IF(T2.PROPERTIES IS NULL, "NA", T2.PROPERTIES)
        ORDER BY T1.ORDER_INDEX ASC
    ) AS DATA
FROM TABLE_1 T1
JOIN TABLE_2 T2
    ON T1.ID=T2.ID
WHERE T1.AUTHOR="123"
GROUP BY T1.PARENT_ID
ORDER BY T1.PARENT_ID;

Mysql版本===>的 5.0.27标日志

以上查询返回:

+-----------+---------------+
| parent_id | data          |
+-----------+---------------+
| 12345     | te,test1,test |
| 23456     | NA            |
+-----------+---------------+
2 rows in set (0.00 sec)

_
但是,它应该是:

+-----------+---------------+
| parent_id | data          |
+-----------+---------------+
| 12345     | NA,test1,test |
| 23456     | NA            |
+-----------+---------------+
2 rows in set (0.00 sec)    

-
   -
  ########################### TABLE DATA ##################### #######

mysql> select * from Table_1;
+----+-----------+--------+-------------+
| id | parent_id | author | order_index |
+----+-----------+--------+-------------+
|  1 | 12345     | 123    |           3 |
|  2 | 12345     | 123    |           1 |
|  3 | 23456     | 123    |           1 |
|  4 | 12345     | 123    |           2 |
+----+-----------+--------+-------------+
4 rows in set (0.00 sec)

mysql> select * from Table_2;
+----+------------+
| id | properties |
+----+------------+
|  1 | test       |
|  2 | NULL       |
|  3 | NULL       |
|  4 | test1      |
+----+------------+
4 rows in set (0.00 sec)

-
   -
  ########################### TABLE MODEL ##################### #######

mysql> desc Table_1;
+-------------+-----------+------+-----+---------+-------+
| Field       | Type      | Null | Key | Default | Extra |
+-------------+-----------+------+-----+---------+-------+
| id          | int(11)   | NO   | PRI |         |       |
| parent_id   | char(100) | YES  |     | NULL    |       |
| author      | char(100) | YES  |     | NULL    |       |
| order_index | int(11)   | YES  |     | 0       |       |
+-------------+-----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> desc Table_2;
+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| id         | int(11) | NO   | PRI |         |       |
| properties | text    | YES  |     | NULL    |       |
+------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

0 个答案:

没有答案