![](/img/trans.png)
[英]How to get the max ID in a Join Query in Laravel 5 (Integrity constraint violation:)
[英]Integrity constraint violation: How to join two tables
我正在為laravel的學校項目制作一個程序,所以我嘗試加入兩個表:產品和目的地
表Product有列:id,name
Destinations表具有以下列:Destinations:id,product_id,destination,quantity,target_person
我需要加入product_id
和id
products = DB::table('products')
->leftJoin('destinations','id','=','destinations.product_id ')
->get();
但是當我嘗試使用LEFT JOIN
我收到以下錯誤:
SQLSTATE [23000]:完整性約束違規:1052列'ID'在條款是不明確的(SQL:SELECT * FROM
products
內部連接destinations
的id
=destinations
。product_id
)
使用表格參考products.id
products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')
->get();
是因為它不知道'id'是指產品或目的地中的那個。 試試這個:
products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.