情况:
产品表、库存表两张表关联查询,由于一个产品有多个库存记录,需查出最新的库存记录
解决方案:
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