我需要在创建区域显示商店以及订单日期。我创建了第三个表来连接已经存在的两个(商店和区域)并使用内部联接加入。首先,我得到了这样的结果:
area name: name1
shops: shop1
date: date1
area name: name1
shops: shop2
date: date1
我需要:
area name: name1
shops: shop1, shop2
date: date1
然后我找到了一些代码并将其包含在脚本中,但它仅适用于一行(下面的代码中的名称)。如果我尝试使用elseif与该代码的日期,它显示错误。代码如下。
<?php
include('db.php');
$result = mysql_query("SELECT * FROM area INNER JOIN area_join ON area.id = area_join.area_id INNER JOIN shops ON area_join.shops_id = shops.id ") or die("Error: " . mysql_error());;
while($data = mysql_fetch_assoc($result)){
if($data['name'] != $groupname){
echo "<br><hr>Area name: ".$data['name']."<br />Shops: ";
$groupname = $data['name'];
}
echo "".$data['shop_name'].", ";
}
?>
答案 0 :(得分:1)
您是否尝试过group_concat? (现在不能测试它,但我认为是这样的)
SELECT areaname,group_concat(shops) FROM area INNER JOIN area_join ON area.id = area_join.area_id INNER JOIN shops ON area_join.shops_id = shops.id
Group by areaname
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
答案 1 :(得分:0)
使用GROUP_CONCAT
,
SELECT areaName,
GROUP_CONCAT(DISTINCT shopname) shopList
FROM area
INNER JOIN area_join
ON area.id = area_join.area_id
INNER JOIN shops
ON area_join.shops_id = shops.id
GROUP BY areaName
将列名称更改为表格中找到的原始名称。