i have some problems with a Query seem IN dosen't work with Group_concat, that is what i wrote
SELECT category, (
SELECT GROUP_开发者_如何转开发CONCAT( DISTINCT `short` SEPARATOR ', ' )
FROM `ods_category`
WHERE cid IN (n.category)
ORDER BY cid
) AS catstring
FROM ods_news AS n
the problem is just the column "catstring" containe only 1st item found in subquery but if i replace
WHERE cid IN (n.category)
with
WHERE cid IN (19,18,3)
it's work well but as u can see that isn't dynamic do u know why n.category limit to just one item?
idk if u need that but a sintetic strucure of tables is
ods_news ods_category
id | category cid | short
1 | 1 1 | AA
2 | 3 2 | BB
3 | 4,5 3 | CC
4 | 1,2,4 4 | DD
5 | 6 5 | EE
6 | 2,1,6 6 | FF
that is the results i have and what i want
results i get result i want
category | catstring category | catstring
1 | AA 1 | AA
3 | CC 3 | CC
4,5 | DD 4,5 | DD,EE
1,2,4 | AA 1,2,4 | AA,BB,DD
6 | FF 6 | FF
2,1,6 | BB 2,1,6 | BB,AA,FF
I created your test data and replaced the IN() with another function. It gives exactly the results you want:
SELECT category, REPLACE((
SELECT GROUP_CONCAT( DISTINCT `short` SEPARATOR ', ' )
FROM `ods_category`
WHERE FIND_IN_SET(cid, REPLACE(n.category, ' ', '')) != 0
ORDER BY cid
), ' ', '') AS catstring
FROM ods_news AS n
Result is:
+----------+------------+
| category | catstring |
+----------+------------+
| 1 | AA |
| 3 | CC |
| 4,5 | DD,EE |
| 1,2,4 | AA,BB,DD |
| 6 | FF |
| 2,1,6 | AA,BB,FF |
+----------+------------+
6 rows in set (0.00 sec)
精彩评论