我有三个表推荐,产品和产品_照片。在recommends表和products_photos表中,它们都有product_id列。我想将它们(三个)连接在一起,但收到此错误 Integrityconstraintviolation: 1052 Column 'product_id' in field list
,如何连接它们?
Route::get('/info', function(){
$products = DB::table('recommends')
->leftJoin('products','recommends.product_id','products.id')
->join('products_photos','products_photos.product_id','products.id'
)
->select('product_id','name','price', DB::raw('count(*) as total'))
->groupBy('product_id','name','price')
->get();
//dd($products);
});
最佳答案
您从未指定在 join 调用中使用的相等/不等运算符。尝试这样做,并使用适当的别名限定您选择的所有列:
Route::get('/info', function() {
$products = DB::table('recommends AS r')
->leftJoin('products AS p', 'r.product_id', '=', 'p.id')
->join('products_photos AS pp', 'pp.product_id', '=', 'p.id')
->select('p.id', 'p.name', 'p.price', DB::raw('COUNT(*) AS total'))
->groupBy('p.id', 'p.name', 'p.price')
->get();
});
我假设 products
具有 id
、name
和 price
列。
关于php - 连接表 MySql 的完整性约束违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963485/