2019-02-14
阅读量:
716
SQL查询问题
问题描述:
店铺表shop
shopid shopname
1 水果店
2 衣服店
3 粮食店
活动表activity
aid shopid actname
1 1 送苹果活动
2 1 送橘子活动
3 2 送衬衫活动
4 2 送鞋子活动
商户表user
商户id 店铺id 商户等级
uid shopid level
87 1 高级商户
88 2 中级商户
89 3 低级用户
需要找到有活动的店铺,然后显示信息,最终结果
水果店 高级用户 有送苹果和送橘子活动
衣服店 中级用户 有送衬衫和送鞋子活动
1.shop表inner join activity表 group by shopid 找到有活动的店铺
shoplist = select shopid inner join activity on a.shopid=s.shopid;
2.然后遍历shoplist
$res = array();
for($shoplist as $val){
$info = select * from user left join activity on u.shopid=a.shopid where shopid=$val['shopid'] //
$info = array(
array(level=>'高级用户',actname='送苹果活动'),
array(level=>'高级用户',actname='送橘子活动'),
);
处理$info
最后店铺信息为 $res[] = array('高级用户,送苹果活动,送橘子活动');
}
能不能直接用一条sql解决这个问题呢?
解决方法:
select shopname,level,group_concat(actname) from(select shop.shopname,user.level,activity.actname from shop inner join user on shop.shopid = user.shopid inner join activity on shop.shopid = activity.shopid) group by shopname,level;






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论