Php 从多对多表中获取对应值和非对应值
我的表结构如下所示:Php 从多对多表中获取对应值和非对应值,php,mysql,sql,zend-framework,zend-framework2,Php,Mysql,Sql,Zend Framework,Zend Framework2,我的表结构如下所示: Brands=>Histories我想你只需要一个左加入和分组: select b.brandId, sum(h.points) from brands b left join histories h on b.brandid = h.brandid and h.userId = $userId group by b.brandId; 如果用户的历史记录中没有行,则该值将为NULL 编辑: 如果使用的是左联接,则分组依据应位于第一个表中的一列上: se
Brands=>Histories我想你只需要一个
左加入和分组:
select b.brandId, sum(h.points)
from brands b left join
histories h
on b.brandid = h.brandid and h.userId = $userId
group by b.brandId;
如果用户的历史记录
中没有行,则该值将为NULL
编辑:
如果使用的是左联接
,则分组依据
应位于第一个表中的一列上:
select b.*, sum(h.points) as points
from brands b left join
histories h
on b.id = h.brandId and
h.userId = 2866 and h.brandId = 2
group by b.brandId;
我想你只需要一个左连接
和分组方式
:
select b.brandId, sum(h.points)
from brands b left join
histories h
on b.brandid = h.brandid and h.userId = $userId
group by b.brandId;
如果用户的历史记录
中没有行,则该值将为NULL
编辑:
如果使用的是左联接
,则分组依据
应位于第一个表中的一列上:
select b.*, sum(h.points) as points
from brands b left join
histories h
on b.id = h.brandId and
h.userId = 2866 and h.brandId = 2
group by b.brandId;
select b.*, sum(h.points) as points
from brands b left join
histories h
on b.id = h.brandId and
h.userId = 2866 and h.brandId = 2
group by b.brandId;