$sql = 'select max(price.price) as max_price,min(price.price) as min_price, c.id,c.pid,c.name,c.pic,p.title,p.id as pro_id,p.title
        from xy_product_cate as c
        left  join xy_product as p on  c.id = p.cid
        left  join xy_price as  price on price.pid = p.id
        where c.pid in ('.$idsStr.')  and c.status=0
        group by c.id';(这里要用总的)

 

一对多

表a
aid atitle atext
1 测试1 测试1测试1测试1测试1测试1
2 测试2 测试2测试2测试2测试2测试2
表b:
bid aid bclass
1 1 red
2 1 green
3 2 red
4 1 white
查询语句:
SELECT a.*,b.bclass FROM aleft JOIN B ON a.aid = b.aid WHERE a.aid=1
结果:
aid atitle atext          bclass
1 测试1 测试1测试1测试1测试1测试1 red
1 测试1 测试1测试1测试1测试1测试1 green
1 测试1 测试1测试1测试1测试1测试1 white
这样在显示bclass数据时,atitle及atext数据就要重复读取。通过下面的语句查询,达到如下一种效果:

 

select a.aid,a.atitle ,a.atext,group_concat(b.bclass)
FROM aleft JOIN B ON a.aid = b.aid WHERE a.aid=1
group by a.aid

 

aid atitle atext            bclass
1 测试1 测试1测试1测试1测试1测试1 red,green,white

 

GROUP_CONCAT(b.bclass SEPARATOR ‘|’)可改变分隔符号。

 

同理,可进行三表查询

SELECT A.*,group_concat(C.name) FROM A
LEFT JOIN B
ON A.id = B.id
LEFT JOIN C
ON B.class_id = C.class_id
GROUP BY a.id

 

group_concat(conv(oct(B.id),8,10 )) 如内容为string 直接group_concat(string),如是整数的,须进行转换。(其它不用的)

 

posted on 2016-12-29 09:46  allen817  阅读(123)  评论(0编辑  收藏  举报