现在有两个表的数据,图中所示,A表中的数据搜索出来作为一个数组,B表中搜索出来作为一个数组,B表中的aid对应A表中的id,想实现一个功能,就是A表中所有的数据都显示出来,如果B表中没有此数据自动添加为0,,图片:想实现的效果如下:
name counts
大连 0
长沙 7
厦门 0
广州 0
写sql语句或者php数组遍历都可以
name counts
大连 0
长沙 7
厦门 0
广州 0
写sql语句或者php数组遍历都可以
id name
1 大连
2 长沙
3 夏门
4 广州
[ select * from B ]
id aid counts
1 2 7
[ select A.name, ifnull(B.counts,0) from A left join B on A.id=B.aid ]
name ifnull(B.counts,0)
大连 0
长沙 7
夏门 0
广州 0