【发布时间】:2017-09-03 21:23:09
【问题描述】:
这里是表架构
客户
------------------------------
id name
------------------------------
1 joe
4 jane
产品
------------------------------
id title
------------------------------
1 iphone
2 ipad
customers_products
------------------------------
id product_id customer_id
------------------------------
1 1 1
2 2 1
3 1 5
4 1 9
价格
-------------------------------------------
id product_id price created_at
-------------------------------------------
3 1 300 2017-04-01
4 2 450 2017-04-01
5 2 500 2017-04-02
6 1 320 2017-04-04
7 1 200 2017-04-05
我想要得到的是按用户 ID 分类的每种产品的最后价格结果,像这样
user_id product_id last_price
1 1 200
1 号产品的最后更新价格(price_history 第 7 行)
这是我迄今为止所做的,它给出了错误的结果
select
id,prices.price as current_price,customers_products as price
from prices
join products on products.id = prices.product_id
join customers_products on customers_products.product_id = prices.product_id
where customers_products.customer_id = 1
group by prices.product_id order by prices.id desc
非常感谢你们的帮助!
谢谢!
【问题讨论】:
-
查询的输出是什么,输出到底有什么问题?
标签: mysql sql group-by aggregation