我正在尝试创建一个查询,在这个查询中,我从一个表中获取数据,并使用INNER JOIN,但在where NOT in中,我可以调整INNER部分,但在Where Not IN中,我尝试使用Laravel 5.4中的WhereNotIn。但它返回错误:为foreach()提供的参数无效
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所以我在我的应用程序中创建了这个:
$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();有什么建议吗?
发布于 2017-05-17 03:45:31
下面这行代码是错误的:
->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')的正确名称。
发布于 2017-05-17 03:52:37
$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();https://stackoverflow.com/questions/44009761
复制相似问题