我需要用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();
发布于 2017-05-17 13:18:03
你有一个错误:
->whereNotIn('erp_product.erp_producid'
应:
->whereNotIn('erp_product.erp_productid'
注意拼写错误的productid
。
调试时可以做的一件事是删除->get()
调用,使$categoria
成为查询对象,然后这样做,您就可以看到组装的查询:
dd($categoria->toSql());
https://stackoverflow.com/questions/44026001
复制相似问题