SQL 查询以获取该用户的最新记录
SQL Query to get latest records for that user
我有一个 MySQL 数据库,我需要一些帮助来查询 table 中的数据。
// Table
id INTEGER,
column1 VARCHAR,
completiondate DATETIME
// Sample data
(101, 'a', '2020-03-20 12:00:00')
(101, 'b', '2020-03-21 12:00:00')
(101, 'c', '2020-03-22 12:00:00')
(101, 'c', '2020-03-23 12:00:00')
(101, 'd', '2020-03-24 12:00:00')
(102, 'a', '2020-03-20 12:00:00')
(102, 'b', '2020-03-21 12:00:00')
在这里,我想查看该特定用户的所有记录,并仅显示在 column1
中找到的重复项中的最新记录。
用户 101
的预期输出:
(101, 'a', '2020-03-20 12:00:00')
(101, 'b', '2020-03-21 12:00:00')
(101, 'c', '2020-03-23 12:00:00')
(101, 'd', '2020-03-24 12:00:00')
我是 SQL 的新手。如果有人能对此提供任何见解,那就太好了。
提前致谢!
您可以使用子查询进行过滤:
select t.*
from mytable t
where
t.id = 101
t.completiondate = (
select max(t1.completiondate)
from mytable t1
where t1.id = t.id and t1.id = t.id and t1.column1 = t.column1
)
或者,在 MySQL 8.0 中,您可以使用 window 函数 rank()
:
select *
from (
select t.*, rank() over(partition by id, column1 order by completiondate desc) rn
from mytable t
where id = 101
) t
where rn = 1
请注意,对于此数据集,您还可以使用简单聚合:
select id, column1, max(completiondate) completiondate
from mytable
where id = 101
group by id, column1
这是一种 PHP 友好的方法,使用连接:
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT id, column1, MAX(completiondate) AS maxcompletiondate
FROM yourTable
GROUP BY id, column1
) t2
ON t1.id = t2.id AND
t1.column1 = t2.column1 AND
t1.completiondate = t2.maxcompletiondate;
我认为最简单的方法是以某种方式将 table 的最大值加入当前 table
SELECT user, `date`
FROM yourtable
INNER JOIN
(
SELECT MAX(date) AS `date`, user
FROM yourtable
GROUP BY user
) latest ON latest.`date`= yourtable.`date` AND latest.user = yourtable.user
我有一个 MySQL 数据库,我需要一些帮助来查询 table 中的数据。
// Table
id INTEGER,
column1 VARCHAR,
completiondate DATETIME
// Sample data
(101, 'a', '2020-03-20 12:00:00')
(101, 'b', '2020-03-21 12:00:00')
(101, 'c', '2020-03-22 12:00:00')
(101, 'c', '2020-03-23 12:00:00')
(101, 'd', '2020-03-24 12:00:00')
(102, 'a', '2020-03-20 12:00:00')
(102, 'b', '2020-03-21 12:00:00')
在这里,我想查看该特定用户的所有记录,并仅显示在 column1
中找到的重复项中的最新记录。
用户 101
的预期输出:
(101, 'a', '2020-03-20 12:00:00')
(101, 'b', '2020-03-21 12:00:00')
(101, 'c', '2020-03-23 12:00:00')
(101, 'd', '2020-03-24 12:00:00')
我是 SQL 的新手。如果有人能对此提供任何见解,那就太好了。
提前致谢!
您可以使用子查询进行过滤:
select t.*
from mytable t
where
t.id = 101
t.completiondate = (
select max(t1.completiondate)
from mytable t1
where t1.id = t.id and t1.id = t.id and t1.column1 = t.column1
)
或者,在 MySQL 8.0 中,您可以使用 window 函数 rank()
:
select *
from (
select t.*, rank() over(partition by id, column1 order by completiondate desc) rn
from mytable t
where id = 101
) t
where rn = 1
请注意,对于此数据集,您还可以使用简单聚合:
select id, column1, max(completiondate) completiondate
from mytable
where id = 101
group by id, column1
这是一种 PHP 友好的方法,使用连接:
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT id, column1, MAX(completiondate) AS maxcompletiondate
FROM yourTable
GROUP BY id, column1
) t2
ON t1.id = t2.id AND
t1.column1 = t2.column1 AND
t1.completiondate = t2.maxcompletiondate;
我认为最简单的方法是以某种方式将 table 的最大值加入当前 table
SELECT user, `date`
FROM yourtable
INNER JOIN
(
SELECT MAX(date) AS `date`, user
FROM yourtable
GROUP BY user
) latest ON latest.`date`= yourtable.`date` AND latest.user = yourtable.user