2

我有这个乘客表日期:

在此处输入图像描述

这意味着在 Reservation000004中,有两个行程,即 Itinerary0000010 (AAC Hangar - Anvaya Cove)0000011 (Anvaya Cove - AAC Hangar),并且在每个行程中,有两名乘客(0000007 (taiga)并且0000011 riyuuji0000010 AAC Hangar - Anvaya Cove while0000008 minori and0000012 ami are passengers of0000011 Anvaya Cove - AAC Hangar 的乘客)。

我正在尝试使用它们来连接它们,group_concat但我只能这样做:

    $a=mysql_query("select group_concat(pass_name separator ', ') as 'pass' from passengers where reservno = '0000004' order by id asc") or die(mysql_error());
    $b=mysql_fetch_array($a);
    $c=$b['pass']; 

结果:taiga, minori, riyuuji, ami

但我希望它看起来像这样: taiga, riyuuji / minori, ami

这可以使用group_concat吗?我知道这将涉及while语句,但到目前为止我仍然无法获得所需的输出。请帮忙。

4

1 回答 1

2

您需要使用GROUP BY子句

select group_concat(pass_name separator ', ') as 'pass' 
from passengers 
where reservno = '0000004' 
GROUP BY reservno, id
order by id asc

更新 1

SELECT  group_concat(`pass` separator ' / ') `pass` 
FROM    ( 
            select group_concat(pass_name separator ', ') as 'pass' 
            from passengers 
            where reservno = '0000004' 
            GROUP BY reservno, id order by id asc 
        ) s
于 2013-03-21T06:07:28.040 回答