MySQL 內聯查詢具有附加過濾的多個表
您正在嘗試使用內聯在 MySQL 中聯接多個表。特別是,您嘗試檢索有關特定使用者的產品、定價和清單的資訊。但是,在查詢中包含清單表會導致輸出中出現額外的不需要的資料。
讓我們分析您的查詢並修改它以解決問題:
SELECT * FROM orders INNER JOIN products_pricing ON orders.pricing_id = products_pricing.id INNER JOIN products ON products_pricing.product_id = products.id WHERE orders.user_id = '7'
此查詢檢索訂單表中 user_id '7' 的所有記錄,包括關聯的定價和產品資訊。
包含列表表並將結果限制為具體列表,您需要添加一個附加條件:
SELECT p.id, p.name, l.url, o.user_id, o.pricing_id FROM orders AS o INNER JOIN products_pricing AS pp ON o.pricing_id = pp.id INNER JOIN products AS p ON pp.product_id = p.id INNER JOIN listings AS l ON l.user_id = o.user_id WHERE o.user_id = '7' AND l.id = 233 -- Add condition to filter by specific listing AND l.url = 'test.com' -- Add condition to filter by specific url
此修改後的查詢在WHERE 子句中添加兩個條件:
此更新的查詢應該為您提供所需的輸出:一個表,其中包含有關user_id '7' 的用戶的產品、定價和列表的信息,已過濾以具體上市條件為準。
以上是如何有效率地過濾多個表的MySQL內連接結果?的詳細內容。更多資訊請關注PHP中文網其他相關文章!