我最近学会了如何使用GROUP_CONCAT命令显示如下所示的结果:
gyrfalcon |冰岛,蒙古 meadowlark |南达科他州,怀俄明州
现在我想知道是否有办法链接结果(地名),如下所示:
meadowlark | <a href="/world/south-dakota" title="South Dakota">South Dakota</a>, <a href="/world/wyoming" title="Wyoming">Wyoming</a>
在此特定示例中,地名存储在GG.Name中,而URL存储在GG.URL中。这是我的疑问:
SELECT PLAN.Latin Latin2, PLAN.Common, PLAN.Rank, PLAN.GroupTax, PLAN.Desig, PLAN.Family, PLAN.Order1, GS.Symbol, GS.Latin, GG.URL AS GURL, GG.Name AS Name, GROUP_CONCAT(GG.Name ORDER BY GG.Name ASC SEPARATOR ', ') 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
WHERE PLAN.Desig = :RefCat AND Rank != '55'
GROUP BY PLAN.Common
ORDER BY PLAN.N, GG.Name
以下是剧本的其余部分:
$PlaceGroup = $row['Names'];
echo '<tr>
<td class="Symbol">> <a href="'.$GZ_URL.'/life/'.$LatinL.'" title="'.$Symbol.' ('.$Latin2.')">'.$row['Common'].'</a> (<em>'.$Latin2.'</em>)</td>
<td class="Place">'.$PlaceGroup.'</td>
</tr>';
P.S。如果我无法从GG.URL插入值,则另一种可能是简单地修改GG.Name中的值。例如,如果GG.Name ='New York',则可以通过简单地将其更改为小写并用短划线替换空格来将其转换为链接。
编辑:这是我的整个修订版查询,显示语法错误:
SELECT PLAN.Latin Latin2, PLAN.Common, PLAN.Rank, PLAN.GroupTax, PLAN.Desig, PLAN.Family, PLAN.Order1, GS.Symbol, GS.Latin, GG.URL AS GURL, GROUP_CONCAT(CONCAT('<a href="', GG.URL, '" title="', GG.Name, '">', GG.Name, '</a>')) as Names, GG.Name, '</a>')) as LISTOFLINKS, 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
WHERE PLAN.Desig = :RefCat AND Rank != '55'
GROUP BY PLAN.Common
ORDER BY PLAN.N, GG.Name
答案 0 :(得分:1)
你可以像这样对网址进行分组:
GROUP_CONCAT(
CONCAT('<a href=\"', GG.URL, '\" title=\"', GG.Name, '\">', GG.Name, '</a>')
ORDER BY GG.Name ASC SEPARATOR ', '
) as LISTOFLINKS
因此,在一行中,您CONCAT
字段构建链接,然后将GROUP_CONCAT
链接转换为单个链接字符串。
(我在字符串中转义了双引号,虽然它似乎取决于配置是否需要)。