gpt4 book ai didi

mysql - Laravel 中使用不同查询进行左连接

转载 作者:行者123 更新时间:2023-11-29 15:36:11 24 4
gpt4 key购买 nike

我有 2 个这样的表:

产品主控:

enter image description here

事件产品:

enter image description here

我使用此查询从product_master 和campaign_product 表中获取数据:

$oSelect = $this->select(
"{$this->table}.product_code",
"{$this->table}.product_display_name",
'dmspro_mys_campaign_product.campaign_code',
'dmspro_mys_campaign_product.product_id',
)->leftJoin(
'dmspro_mys_campaign_product',
\DB::raw("SELECT DISTINCT product_id FROM dmspro_mys_campaign_product"), '=', "{$this->table}.product_id"
);

return $oSelect->get();

但是正如您所看到的,在我的campaign_product表中,有2行相同的product_id,所以我的结果是重复的行:

enter image description here

现在我希望它通过不同的查询仅返回 1 个product_id。我试过这个:

 ->leftJoin(\DB::raw("(SELECT DISTINCT product_id FROM dmspro_mys_campaign_product) as cp"), 'cp.product_id', '=', "{$this->table}.product_id");

但是我的查询语法有错误。

我怎样才能做到这一点?

最佳答案

如果您使用的是 MySQL,请在运行主查询之前运行以下查询。

SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY', ''));

关于mysql - Laravel 中使用不同查询进行左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58216579/

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