使用 Inner Join 連接 MySQL 中的多個表格
在 MySQL 中,INNER JOIN 語句允許您基於共同專欄。要執行多表聯接,您可以使用以下語法:
SELECT columns FROM table_1 INNER JOIN table_2 ON table_1.column = table_2.column [...] INNER JOIN table_n ON table_n-1.column = table_n.column;
考慮以下場景:您有四個表:
原始查詢:
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';
已修訂查詢:
要包含每個產品的URL,您可以在清單表中新增另一個INNER JOIN: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 AND l.url = 'test.com';
以上是如何有效率地連接MySQL中的多個表來檢索相關資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!