gpt4 book ai didi

php - 从多对多表中返回缺失的结果

转载 作者:行者123 更新时间:2023-11-30 22:17:45 26 4
gpt4 key购买 nike

我的表结构如下:

Brands => BrandUser <= Users

我需要获取在 BrandUser 表中有相应记录的品牌和在 BrandUser 表中没有相应记录的品牌......

我尝试了以下查询:

public function getUserBrands($userId) {
$select = new Select();
$select->from(array('bu' => $this->table));
$select->join(array('b' => 'brands'), 'bu.brandId = b.id', array('id','name'));
$select->join(array('u' => 'users'), 'u.id = bu.userId', array('id','username'),Select::JOIN_LEFT);
$where = new Where();
$where->equalTo("bu.userId",$userId);
$select->where($where);
return $this->branduserTable->selectWith($select)->toArray();
}

但我只获取在 BrandUser 表中有相应记录的用户...我需要获取其余在 BrandUser 表中没有相应值的品牌...我该怎么做??

最佳答案

请注意,我不熟悉 Zend-Framework,因此您可能需要稍微调整一下。但是您需要使用 Brands 作为主表/第一个表,以便它可以首先获取该表的所有记录,然后将其与其余表进行匹配。

public function getUserBrands($userId) {
$select = new Select();
$select->from(array('b' => 'brands'));
$select->join(array('bu' => $this->table), 'bu.brandId = b.id', array('id','name'),Select::JOIN_LEFT);
$select->join(array('u' => 'users'), 'u.id = bu.userId', array('id','username'),Select::JOIN_LEFT);
$where = new Where();
$where->equalTo("bu.userId",$userId);
$select->where($where);
return $this->branduserTable->selectWith($select)->toArray();
}

关于php - 从多对多表中返回缺失的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37681800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com