首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Laravel 5.4中使用'Where Not In‘?

如何在Laravel 5.4中使用'Where Not In‘?
EN

Stack Overflow用户
提问于 2017-05-17 03:14:28
回答 2查看 1.7K关注 0票数 0

我正在尝试创建一个查询,在这个查询中,我从一个表中获取数据,并使用INNER JOIN,但在where NOT in中,我可以调整INNER部分,但在Where Not IN中,我尝试使用Laravel 5.4中的WhereNotIn。但它返回错误:为foreach()提供的参数无效

代码语言:javascript
运行
复制
SELECT
        em.erp_mlbid AS category_id

            FROM 
                erp_product AS ep
            INNER JOIN
                erp_product_category AS epc ON epc.erp_productid = ep.erp_productid
            INNER JOIN
                erp_mlbcategory_erpcategory AS emc ON emc.erp_categoryid = epc.erp_categoryid
            INNER JOIN
                erp_mlb_category AS em ON em.erp_mcid = emc.erp_mlbcategoryid    
            WHERE
                ep.erp_productid NOT IN (
                        SELECT
                            epm.erp_productid
                        FROM
                            erp_product_to_mlb AS epm
                )
                AND ep.erp_quantity > 0
                AND ep.erp_status > 0
            LIMIT
                    10,10

所以我在我的应用程序中创建了这个:

代码语言:javascript
运行
复制
$categoria = DB::table('erp_product')
                            ->join('erp_product_category','erp_product_category.erp_productid', '=', 'erp_product.erp_productid')
                            ->join('erp_mlbcategory_erpcategory', 'erp_mlbcategory_erpcategory.erp_categoryid', '=','erp_product_category.erp_categoryid')
                            ->join('erp_mlb_category', 'erp_mlb_category.erp_mcid', '=', 'erp_mlbcategory_erpcategory.erp_mlbcategoryid')
                            ->select('erp_mlb_category.erp_mlbid')
                            ->whereNotIn('erp_product.erp_productid', function($query){
                                $query->select('erp_productid')
                                       ->from('erp_product')
                                       ->where('erp_productid', '=', 'erp_product_category.erp_productid');
                            })
                            ->get();

有什么建议吗?

EN

回答 2

Stack Overflow用户

发布于 2017-05-17 03:45:31

下面这行代码是错误的:

代码语言:javascript
运行
复制
->whereNotIn('erp_product.erp_productid', function($query){
  $query->select('erp_productid')
  ->from('erp_product')
  ->where('erp_productid', '=', 'erp_product_category.erp_productid');
})

  • 删除此行,因为此处没有连接。
  • 您不能说->whereNotIn('erp_product.erp_productid'$query->select('erp_productid')->from('erp_product') in same order whereNotIn来检查second_column中的值不存在于相同的first_column.

因此,清除此行->where('erp_productid', '=', 'erp_product_category.erp_productid')并选中要放入$query->select('right_column_name')->from('right_table_name')的正确名称。

票数 0
EN

Stack Overflow用户

发布于 2017-05-17 03:52:37

代码语言:javascript
运行
复制
$ids= DB::table('erp_product')->pluck('erp_productid')

$categoria = DB::table('erp_product')
                            ->join('erp_product_category','erp_product_category.erp_productid', '=', 'erp_product.erp_productid')
                            ->join('erp_mlbcategory_erpcategory', 'erp_mlbcategory_erpcategory.erp_categoryid', '=','erp_product_category.erp_categoryid')
                            ->join('erp_mlb_category', 'erp_mlb_category.erp_mcid', '=', 'erp_mlbcategory_erpcategory.erp_mlbcategoryid')
                            ->select('erp_mlb_category.erp_mlbid')
                            ->whereNotIn('erp_product.erp_productid',$ids)
                            ->get();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44009761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档