簡體   English   中英

內部連接錯誤 - where 子句中的列“id”不明確

[英]Inner join error - Column 'id' in where clause is ambiguous

我做了一個產品過濾器,但我必須更新。

之前,url 被傳遞了一個 slug 和一個 id。 我使用這個 id 來獲取子類別,並在這個子類別表中保存了一個名為“line_id”的列。 我獲取了在 url 中傳遞的 id,並尋找與此 line_id 相等的內容。 此 line_id 用於產品頁面,其中有一些類別以及與每個類別對應的子類別。

現在我必須做不同的事情,我必須只取一個子類別和 select 對應於該子類別的產品。

在 products 表中,已經保存了每個子類別的 id。 所以我手動測試了它,傳遞了一個子類別的 id,但是在進行內部連接時它給出了一個錯誤。

public function nossas_marcas($slug, $id)
    {
        $menu_path = 'nossas_marcas';
        $category = Categorie::find($id);
        $lines = Line::where('situation', true)->get();

        $ProductsCategories = ProductsCategory::where('id', 3);
        $brandProducts = $ProductsCategories
                            ->join('products', 'products_categories.id', '=', 'products.product_category_id')->get();
        // $brandProducts = $ProductsCategories
        //                     ->join('products', 'products_categories.id', '=', 'products.product_category_id')
        //                     ->take(4)->orderBy(\DB::Raw('rand()'))->get(['products_categories.id', 'products_categories.title as product_category_name', 'products.title as product_title', 'products.id', 'products.ecommerce_link']);

        return view('site.Nossas-marcas.index', compact(['menu_path', 'category', 'lines', 'brandProducts']));
    }

錯誤

Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `products_categories` inner join `products` on `products_categories`.`id` = `products`.`product_category_id` where `id` = 3)

我無法理解錯誤的原因,請有人向我解釋。

你應該告訴他你的意思是什么表。 正確的形式是products_categories.id ,而不是id

ProductsCategory::where('id', 3);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM