我有这样的查询:
SELECT a.sec_id as sid, r.rank
FROM `0_area` AS a
LEFT JOIN `0_rank` AS r ON a.rank_id = r.id
WHERE a.user_id = $id
GROUP BY a.sec_id
返回包含两列的行。为:
sid | rank
2 | user
3 | mod
4 | admin
我想在另一个查询中使用它,同时将两个返回的行放在一列中,可能使用group_concat,如本例所示。以下示例暂时不起作用
SELECT m.id, m.nom, a.sec_id, a.rank, GROUP_CONCAT( cast( DISTINCT a.sec_id # r.rank
ORDER BY a.sec_id ASC
SEPARATOR ',' ) AS char ) AS levels
FROM `0_member` AS m
LEFT JOIN `0_area` AS a ON m.id = a.user_id
LEFT JOIN `0_rank` AS r ON r.id = a.rank_id
WHERE a.sec_id =2
ORDER BY m.nom ASC
感谢。
PS。我得到的错误是:#1064 - 您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'DISTINCT a.sec_id#r.rank ORDER BY a.sec_id ASC SEPARATOR',')附近使用正确的语法作为第1行的lev'
答案 0 :(得分:10)
这解决了它。
GROUP_CONCAT( cast( concat( a.sec_id, \': \', a.rank ) AS char ) SEPARATOR \', \') AS ranks