我的表格看起来像:
# Table user
user_id PK
...
# Table buy
buy_id PK
user_id FK
...
# Table offert
offert_id
user_id
...
好吧,我需要知道 1 个“用户”的最后一次“购买”并获得该“用户”的“报价”计数,我尝试了类似的方法:
select b.buy_id,count(distinct c.offert_id) as cv from user a
inner join buy b using(user_id) left join offert c using(user_id) where a.user_id=4
group by a.user_id order by b.buy_id desc
但它总是返回第一个“购买”而不是最后一个,看起来这个顺序没有任何效果
我知道我可以使用子查询来做到这一点,但我想知道是否有办法使用子查询来做到这一点,也许使用 max 函数,但我想知道如何做到这一点。
谢谢。