Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从多对多表中获取对应值和非对应值_Php_Mysql_Sql_Zend Framework_Zend Framework2 - Fatal编程技术网

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;