Query for latest price per seller per product [duplicate]

£可爱£侵袭症+ 提交于 2020-06-01 05:10:51

问题


I have a database table which looks as following:

id    product_id     product_name     price     date            seller
1     646            Product 1        1         2020-05-20      Seller-A
2     1554           Product 2        1.50      2020-05-23      Seller-B
3     646            Product 1        2         2020-05-22      Seller-C
4     646            Product 1        2.5       2020-05-23      Seller-A

As a result I would like to get the latest info based on the date for every product_id.

 2     1554           Product 2        1.50      2020-05-23      Seller-B    
 4     646            Product 1        2.5       2020-05-23      Seller-A

I have the following query which works fine for 1 product id:

SELECT * FROM (SELECT * FROM `table` WHERE 1 ORDER BY `date` DESC) t2 WHERE product_id = 646 GROUP BY `seller`

How should I build the query to get the results for multiple id's. At the moment it will only return the result for every seller available but no matter how many product ids are available

来源:https://stackoverflow.com/questions/62005692/query-for-latest-price-per-seller-per-product

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!