Heim >Backend-Entwicklung >PHP-Tutorial >多表关联调用,有点复杂,高手帮忙下谢谢了
表 goods_area
字段 goods_id city area
1 北京 大望路
表 business_circle
字段 id name parent_id
此表为地区商圈的父类子类表
id name parent_id
1 朝阳 0
2 昌平 0
3 大望路 1
就是'大望路'是'朝阳'的子类parent_id 就是其父类的id号
两个表的关联字段是a.area=b.name
$sql = mysql_query("select * from goods_area as a INNER JOIN business_circle as b on a.area=b.name order by a.goods_id asc");
while($rs = mysql_fetch_assoc($sql)){
echo $rs['area'].$rs['parent_id'].'
';
}
这样能调用处parent_id的值但我想调用出条件parent_id = id表business_circle里的name值
而我想调出来的结果是 a表$rs['area']里b表父类name的值
不知道讲的清楚吗?高手直接给完整代码,谢谢了
可能需要两次循环把得到$rs['parent_id']的值再作为条件调用,请问高手代码应当怎么写
select c.name,d.* from goods_area as c,(goods_area select * from goods_area as a INNER JOIN business_circle as b on a.area=b.name) as d where c.id=d.parent_id
select c.name,d.* from business_circle as c,(select * from goods_area as a INNER JOIN business_circle as b on a.area=b.name) as d where c.id=d.parent_id
应该是这个,c.name就是你要的name