情況:
產品表、庫存表兩張表關聯查詢,由於一個產品有多個庫存記錄,需查出最新的庫存記錄
解決方案:
SELECT
a.hotel_id '酒店ID',
a.ticket_product_id '產品ID',
a.sell_date_start '銷售開始時間',
a.sell_date_stop '銷售結束時間',
a.sale_price '售價',
t.init_count '總庫存',
t.day_limit_count '每日限量庫存'
FROM
ticket_product a
LEFT JOIN (
SELECT
b.*
FROM
product_inventory b
LEFT JOIN ( SELECT max( c.product_inventory_id ) AS product_inventory_id, c.product_id FROM product_inventory c GROUP BY c.product_id ) d ON b.product_inventory_id = d.product_inventory_id
) t ON a.ticket_product_id = t.product_id