如何按最大日期连接表和 select

How to join tables and select by max date

我有这张表,一张有产品,另一张有产品价格,但它按日期变化,所以我想要一张 select 有产品和最后价格。

我做了这个 select 但它 returns 所有产品和所有价格 我只想要插入最后价格的所有产品

SELECT tbl_product.*, tbl_product_price.price FROM tbl_product JOIN tbl_product_price ON tbl_product.id = tbl_product_price.FK_id_product
tbl_product
id, product
 1   bread
 2   soda
 3   milk

tbl_product_price
FK_id_product, price,   last_date_change
     1          0.11   2020-09-15 17:04:41
     1          0.12   2020-09-16 09:13:53
     1          0.13   2020-09-17 12:20:25
     2          0.65   2020-09-15 20:00:07
     2          0.69   2020-09-16 11:33:40
     3          0.91   2020-09-15 02:54:32
     3          1.00   2020-09-16 13:33:22
     3          0.95   2020-09-17 15:41:11

我的加急查询结果

id, product,  price
 1   bread    0.13
 2   soda     0.69
 3   milk     0.95

我希望我解释得很好 谢谢

相关子查询可能会派上用场:

select 
    p.*,
    (
        select pp.price 
        from tbl_product_price pp 
        where pp.fk_id_product = p.id 
        order by pp.last_date_change desc limit 1
    ) as price
from tbl_product p

为了提高此查询的性能,请考虑 tbl_product_price(fk_id_product, last_date_change desc, price).

上的索引