我有两个要连接的表,条件如下:只显示产品类型为1的产品,并且产品应该在img tbl中可用,并且不应该有noImg.jpg
products tbl img tbl
+-------------------------+ +--------------------------------+
| id | product_type | id | product_id | file
--------------------------- +---------------------------------
| 123 | 1 | 1 |123 | 1.jpg
| 234 | 2 | 2 |234 | noImg.jpg
| 345 | 1 | 3 |345 | 3.jpg我在Laravel中执行了以下操作,但查询似乎不正确,因为它没有给我正确的结果。
Product::join('img', function($join) {
$join->on('products.id', '=', 'img.product_id')
;
})
->where('img.file', '!=', 'noImg.jpg')
->where('products.product_type', 1)
->paginate(100);发布于 2019-12-12 17:58:20
尝尝这个
Product::rightJoin('img', function($join) {
$join->on('products.id', '=', 'img.product_id')
;
})
->where('img.file', '<>', 'noImg.jpg')
->where('products.product_type','=',1)
->get();发布于 2019-12-12 17:58:12
试试这段代码
Product::rightJoin('img', function($join) {
$join->on('products.id', '=', 'img.product_id')
->where('img.file', '!=', 'noImg.jpg');
})
->where('products.product_type', '=', 1)
->paginate(100);我相信img和products是您的表名。
https://stackoverflow.com/questions/59301681
复制相似问题