如何在Laravel中执行此操作,子查询不在

时间:2017-05-17 13:15:05

标签: mysql laravel

我需要在Laravel格式中转换此查询,我尝试使用'WhereNotIn',但我没有得到我想要的结果。有人可以帮帮我吗?

SELECT
em.erp_mlbid AS category_id

FROM
erp_product AS ep
INNER JOIN
erp_product_description AS epd ON epd.erp_productid = ep.erp_productid
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
INNER JOIN
erp_product_image AS epi ON epi.erp_productid = ep.erp_productid
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
                    1

显然我的行'WhereNotIn'被忽略,结果是相同的:

 $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($q){
                                $q->select('erp_productid')->from('erp_product_to_mlb');
                            })
                            ->get();

1 个答案:

答案 0 :(得分:3)

你有一个错字:

->whereNotIn('erp_product.erp_producid'

应该是:

->whereNotIn('erp_product.erp_productid'

注意拼写错误的productid

您可以执行的一项调试是删除->get()调用以使$categoria成为查询对象,然后执行此操作以便查看已汇编的查询:

dd($categoria->toSql());