将订单子句添加到GROUP_CONCAT

时间:2014-09-05 01:15:25

标签: php mysql

如何修改以下内容以便按字母顺序显示地名(GG.Name)?

GROUP_CONCAT(CONCAT('<a href=\"', GG.URL, '\" title=\"', GG.Name, '\">', GG.Name, '</a>')) AS Names,

我原来的GROUP_CONCAT条款包含&#34; ORDER BY&#34;,但我无法使用修改后的代码。

另外,你知道如何修改它以便每个逗号后面都有空格吗?它显示亚利桑那州,加利福尼亚州我实际上用一个简单的str_replace函数修复了它,但我只是想知道是否有一种方法可以在没有修复的情况下正确显示它。

修改

我尝试了以下两种变体,但它们都会产生语法错误......

SELECT PLAN.Latin Latin2, PLAN.Common, PLAN.Rank, PLAN.Planimal, PLAN.Family, PLAN.Order1, PLAN.NoSymbol, PLAN.National, GS.Symbol, GS.Latin, GG.URL AS GURL, GROUP_CONCAT(CONCAT('<a href=\"', GG.URL, '\" title=\"', GG.Name, '\">', GG.Name, '</a>' ORDER BY GG.Name SEPARATE ',  ')) AS Names, GG.IDParent
FROM gs_planimals PLAN
LEFT JOIN gs GS ON GS.Latin = PLAN.Latin
LEFT JOIN gw_geog GG ON GG.IDArea = GS.IDArea AND GG.IDParent = :DBParent
WHERE Rank != 55 AND PLAN.National = 'nat' AND NoSymbol != '1'
GROUP BY PLAN.Latin
ORDER BY PLAN.N, GG.Name

版本#2:

SELECT PLAN.Latin Latin2, PLAN.Common, PLAN.Rank, PLAN.Planimal, PLAN.Family, PLAN.Order1, PLAN.NoSymbol, PLAN.National, GS.Symbol, GS.Latin, GG.URL AS GURL, GROUP_CONCAT(CONCAT('<a href=\"', GG.URL, '\" title=\"', GG.Name, '\">', GG.Name, '</a>') ORDER BY GG.Name SEPARATE ', ') AS Names, GG.IDParent
FROM gs_planimals PLAN
LEFT JOIN gs GS ON GS.Latin = PLAN.Latin
LEFT JOIN gw_geog GG ON GG.IDArea = GS.IDArea AND GG.IDParent = :DBParent
WHERE Rank != 55 AND PLAN.National = 'nat' AND NoSymbol != '1'
GROUP BY PLAN.Latin
ORDER BY PLAN.N, GG.Name

1 个答案:

答案 0 :(得分:1)

语法很好documented。请注意,您甚至不需要concat(),因为group_concat()可以使用多个参数。所以,如果我理解正确的话:

GROUP_CONCAT('<a href=\"', GG.URL, '\" title=\"', GG.Name, '\">', GG.Name, '</a>'
              ORDER BY GC.Name SEPARATOR ', ') AS Names,