MySQL 选择不同的问题
我想返回表中的所有列,但仅限于“唯一”或“不同”用户。
我想从表中获取所有不同的用户,但返回所有列。
我将按时间戳对表进行排序,以便获取每个不同用户的“第一个”条目。
有什么想法吗?
I want to return all columns from a table, but only for the 'unique' or 'distinct' users.
I want to grab all the distinct users from a table, but return all columns.
I will be sorting the table by a timestamp, so as to grab the 'first' entry of each distinct user.
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该发布表定义,但或多或少应该可以使用
GROUP BY
。如果任何其他列也包含变量数据(它们实际上应该在标准化表中),则必须选择要对它们使用的聚合函数。
You should post the table definition but more or less it should be possible using a
GROUP BY
.If any of your other columns also contains variable data (as they actually should in a normalized table), you have to make a choice on what aggregrate function you wish to use on them.
不要忘记重命名用户 ID 和时间以匹配您的列名称
Don't forget to rename userid and time to match your column names
您是否尝试过使用 GROUP BY,即
通常它应该可以工作,但您没有提到任何结构..
Have you tried using GROUP BY, i.e.
Generally it should work, but you haven't mentioned any structure..
其实没必要组团。您想要的是选择表中没有具有较小(或较大)时间戳的同一用户的其他记录的每条记录,
请确保在
user
和time列
You don't really need to group. What you want is select every record in your table that doesn't have another record for the same user with a lesser (or greater) timestamp
Be sure to have indexes on
user
andtime
columns