mysql group_concat里面的同时

时间:2013-03-21 06:05:10

标签: php mysql group-concat

我有这个乘客表日期:

enter image description here

这意味着在预订000004中,有两个行程是行程0000010 (AAC Hangar - Anvaya Cove)0000011 (Anvaya Cove - AAC Hangar),每个行程中都有两名乘客(0000007 (taiga)和{ {1}} 0000011 riyuuji 0000008 minori 0000010 AAC Hangar - Anvaya Cove while 0000012 ami and 0000011 Anvaya Cove - AAC Hangar)的乘客。

我正在尝试使用are passengers of连接它们,但我只能这样做:

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语句,但截至目前我仍然无法获得所需的输出。请帮忙。

1 个答案:

答案 0 :(得分: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