由于 mysql 不支持窗口函数,我只能建议这样做,希望 product_table 中没有两个具有相同日期的产品。因为我没有可用的正在运行的 mysql,所以我不确定这是否有效。
select c.category_name
, p.product_name
, p.color
, p.category_id
, p.sub_categories
, p.detail
, p.[date]
, p.[id]
, p.bst_seller
, p.size
from products p
join ( select category_id
, max([date])
from product_table
) d
on p.[date] = d.[date]
join category_table
on p.category_id = c.[id]
旧版本 - 仅适用于 sql-server
尝试使用 row_number 窗口函数来选择每个类别的最新产品,然后加入:
with products as (
select product_name
, color
, category_id
, sub_categories
, detail
, [date]
, [id]
, bst_seller
, size
, ROW_NUMBER() over (partition by category_id order by date desc) as cand
from product_table
)
select c.category_name
, p.product_name
, p.color
, p.category_id
, p.sub_categories
, p.detail
, p.[date]
, p.[id]
, p.bst_seller
, p.size
from products p
join category_table c
on p.category_id = c.id
where p.cand = 1