gpt4 book ai didi

php - 显示特定字段的所有结果以及带有内连接的其余结果

转载 作者:行者123 更新时间:2023-11-29 14:00:46 25 4
gpt4 key购买 nike

我需要显示创建区域中的商店以及订购日期。我创建了第三个表来连接已经存在的两个表(商店和区域),并使用内部连接将其连接起来。首先,我得到这样的结果:

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'].", ";
}
?>

最佳答案

你尝试过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

关于php - 显示特定字段的所有结果以及带有内连接的其余结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15103565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com