-1
下面是表模式如何從數據透視表拿到最後更新的價格在MYSQL
客戶
------------------------------
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
真正會感謝你的幫助!
謝謝!
什麼是你查詢的輸出,而究竟什麼是錯的輸出元組? –