MySQL GROUP BY 某些字段的最新值
我有一个这样的表:
ID Type UserID Timestamp
1 1 1000 1312389223
2 1 1000 1312389220
3 2 1000 1312389215
4 3 1000 1312389213
5 3 2000 1312389205
6 1 2000 1312389201
7 2 1000 1312389190
我现在需要的是仅检索该表中数据的某些部分。该列表显示与特定用户在我的网站上执行的操作相关的数据。 某些“类型”需要显示所有条目。其他人只需要显示最新的。
在上面的示例中,我需要显示所有出现的 Type = 2,但仅显示 Type = 1 的前 1 个(按时间戳排序)。 Type = 3 也是如此。这是基于每个 UserID 的。
希望我已经解释得足够清楚了。
预先感谢您的帮助, 费舍尔。
I have a table like this:
ID Type UserID Timestamp
1 1 1000 1312389223
2 1 1000 1312389220
3 2 1000 1312389215
4 3 1000 1312389213
5 3 2000 1312389205
6 1 2000 1312389201
7 2 1000 1312389190
What I need now, is to retrieve only certain parts of the data in this table. The list shows data in relation to what a certain user has been doing on my site.
Some of the "Type"'s need to have all entries shown. Others need only to have the most recent shown.
In the above example I need to have all occurances of Type = 2 shown, but only top 1 (ordered by Timestamp) of Type = 1 shown. Same goes for Type = 3. This is per UserID basis.
Hope I have explained myself clear enough.
Thanks in advance for your help,
fischer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不需要ID,则简单而快速的解决方案
将保留ID,但它更复杂并且速度稍慢:
无论如何,这两种解决方案都不是很有可扩展性
Simple and quick solution if you dont need IDs
That will keep IDs, but it's more complicated and somewhat slower:
Anyway, both solutions are not very scalable