如何用逗号将不同的行值放在td中

时间:2014-06-15 07:36:33

标签: php mysql

我有MySQL查询表

customer_id  name    service
1              a        sms
2              b       inbox
1              a       2-way

这里customer_id 1有两个数据,当我写查询从数据库中检索数据并在表中显示它只取第一个值for example customer_id 1 having sms and inbox it only displaying inbox not displaying sms,但我想显示两者这个(短信,双向),可以任何一个gude怎么做。

我的代码:

<?php

                    $sql_selectcustomer = "SELECT customer_id,name FROM `customer` where reseller_id=1 ";

                    $result1 = mysql_query($sql_selectcustomer);

while($rows=mysql_fetch_assoc($result1))
{



    $customer_id=$rows['customer_id'];
    $customer_name=$rows['name'];

    $sql_service = "SELECT product FROM `products`  where customer_id='$customer_id'";
    //print $sql_service;
    $result_service = mysql_query($sql_service);
    while($row1=mysql_fetch_assoc($result_service))
{

    $product = $row1['product'];
    //print $product;
}

 echo '<tr>';

   echo ' <td class="edit name '.$rows["customer_id"].'">'.$customer_name.'</td>
            <td class="edit product '.$rows["customer_id"].'">'.$product.'</td>  


        </tr>';


}


?>

1 个答案:

答案 0 :(得分:1)

使用GROUP_CONCAT

SELECT customer_id,GROUP_CONCAT(service) as service 
FROM `products`  
WHERE customer_id='$customer_id' 
GROUP BY customerid

示例结果:

customer_id     service
-------------------------
1               sms,2-way