获取数据库中最新添加的记录

发布于 2025-01-03 14:08:12 字数 688 浏览 1 评论 0原文

我只能通过例子来描述这个问题.. 如果我有此数据,

    use-id         user-name          add-date
---------------------------------------------------
    2              sami               17/1/2011
    2              sami               18/1/2011
    3              alaa               18/1/2011
    4              jamal              19/1/2011

我想为每个用户选择最新的行,我希望得到此数据:

    use-id         user-name          add-date
---------------------------------------------------
    2              sami               18/1/2011
    3              alaa               18/1/2011
    4              jamal              19/1/2011

对于每个唯一的用户 ID,我想获取最新添加的记录。如何 ?

I can only describe the question by example..
If I have this data

    use-id         user-name          add-date
---------------------------------------------------
    2              sami               17/1/2011
    2              sami               18/1/2011
    3              alaa               18/1/2011
    4              jamal              19/1/2011

I want to select the newest row for each user, I want this data to result :

    use-id         user-name          add-date
---------------------------------------------------
    2              sami               18/1/2011
    3              alaa               18/1/2011
    4              jamal              19/1/2011

for each unique user-id I want to get the newsest added record. how ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽 2025-01-10 14:08:12

一种方法是获取每个用户最新记录的日期:

select `use-id`, max(`add-date`) as `latest` 
from <table_name> 
group by `use-id`

我们将调用该查询 newest_records,并使用它来仅选择具有最新日期的那些行:

select a.*
from <table_name>
inner join newest_records b
on a.`use-id` = b.`use-id` and a.`add-date` = b.`latest`

编辑:

将它们放在一起(复制/粘贴),我们有:

select a.*
from <table_name>
inner join (  
  select `use-id`, max(`add-date`) as `latest` 
  from <table_name> 
  group by `use-id`) b
on a.`use-id` = b.`use-id` and a.`add-date` = b.`latest`

One way is to get the date of the newest record for each user:

select `use-id`, max(`add-date`) as `latest` 
from <table_name> 
group by `use-id`

We'll call that query newest_records, and use it to select only those rows that have the latest date:

select a.*
from <table_name>
inner join newest_records b
on a.`use-id` = b.`use-id` and a.`add-date` = b.`latest`

Edit:

Putting it all together (copy/paste), we have:

select a.*
from <table_name>
inner join (  
  select `use-id`, max(`add-date`) as `latest` 
  from <table_name> 
  group by `use-id`) b
on a.`use-id` = b.`use-id` and a.`add-date` = b.`latest`
盗琴音 2025-01-10 14:08:12

据我所知,User-Id 决定了User-NameUser-Id --> User-Name)。这意味着不会有两个相同的 User-Id 具有不同的 User-Name 。您的数据通过 User-Id 数字 2 清楚地显示了这一点,它们都具有相同的 User-Name (sami)。

因此,进行更简单、更高效的查询是有效的:

select user-id, user-name, max(add-date) from table1
group by user-id, user-name

注意:MySql 还允许您从组中删除 user-name 现在

,显然您的表缺少 标准化。您的表应如下所示:

YourTable(User-Id, Add-Date)

您还应该有另一个表 Users ,应如下所示:

Users( User-Id, User-Name)

为了在这个新模式中获得您期望的结果,您应该连接两个表。

As I see it User-Id determines User-Name (User-Id --> User-Name). That means there won't be two equal User-Ids with different User-Names. And your data clearly shows this with User-Id number 2, which both have the same User-Name (sami).

So it would be valid to do a simpler and more efficient query:

select user-id, user-name, max(add-date) from table1
group by user-id, user-name

Note: MySql also allows you to remove the user-name from the group by

Now, clearly your table is lacking Normalization. Your table should look like this:

YourTable(User-Id, Add-Date)

And you should also have another table Users that should look like this:

Users(User-Id, User-Name)

And to get the result you're expecting in this new schema you should joing both tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文