使用this SO answer我创建了以下查询:
select
created, property_id, requesting_user_id, type, response
from
pdr t1
where
t1.created = (
select max(created)
from pdr t2
where t2.property_id = t1.property_id and t2.requesting_user_id = t1.requesting_user_id
)
这就像一个魅力,但现在我想将这个(也就是我在上面链接的SO答案中建议的)转换为使用连接的查询。所以我想出了这个:
select
created, property_id, requesting_user_id, type, response
from
pdr t1
inner join (
select max(created) as created, property_id, requesting_user_id
from pdr
group by property_id, requesting_user_id
) as t2 on t2.property_id = t1.property_id and t2.requesting_user_id = t1.requesting_user_id and t2.created = t1.created
不幸的是,这会返回错误ambiguous column name: created
。所以我把t1.
或t2.
放在一些创建的东西之前,但后来我得到了各种语法错误,所以我有点迷失在这里。
有人能帮助我解决我在这里做错的事吗?欢迎所有提示!
ps:我目前正在SQLite上测试它,但最终它也适用于MySQL。如果存在差异,那当然也很有趣。
答案 0 :(得分:1)
首先选择应该是:
SELECT t1.created, t1.property_id, t1.requesting_user_id, type, response...
你正确完成的其他事情......
基于https://dev.mysql.com/doc/refman/5.1/en/example-maximum-column-group-row.html ...
GL