【问题标题】:Need to get latest record for two values, then use that to get the oldest -MySQL 5.7需要获取两个值的最新记录,然后使用它来获取最旧的 -MySQL 5.7
【发布时间】:2023-03-23 14:01:01
【问题描述】:

更新:我正在使用的服务器版本是 MySQL 5.7

我已经掌握了 SQL 的基本知识,但是一旦涉及到多个内部连接和查询,我就完全不知道我需要做什么。

所以,我需要做两件事。

  • 首先我需要获取配置文件和类型的最新记录,因为配置文件可以有 a 或 b 类型
  • 有了这些信息,然后我需要在该配置文件和类型在表中时获取最早的记录

因为我知道我已经解释得很糟糕,这里我有两个表的设置信息

SELECT a.profile_id,a.id, a.created_at, b.type
FROM tableA a
LEFT JOIN tableB b ON a.p_id = b.id
a.profile a.id a.created_at b.type
1325586 340807 2021-08-05 21:30:55 A
1325586 340803 2021-08-05 21:28:11 A
1325586 340805 2021-08-05 21:28:11 A
1325487 340707 2021-06-26 13:42:19 B
1325487 340453 2021-03-25 10:49:40 A
1325487 340451 2021-03-25 10:49:39 A

有了上面的信息我想得到以下两条记录

a.profile a.id a.created_at b.type
1325586 340803 2021-08-05 21:28:11 A
1325487 340707 2021-06-26 13:42:19 B

所以它需要首先找出最新的类型与配置文件,然后获取使用该类型的第一条记录。这有意义吗?

我尝试了几种方法,但在某个时候我被难住了。我只有以下内容,这将根据 max(id) 获取我的记录

SELECT a.profile,a.id, a.created_at, b.type
FROM tableA a
LEFT JOIN tableB b ON a.p_id = b.id 
WHERE (a.id, a.profile) in 
(Select max(aa.id), aa.profile from tableA aa 
GROUP BY aa.profile)

【问题讨论】:

  • 这会更容易和更有效的窗口函数,这是可用的 MySql 8.0 及更高版本
  • 同意,但我只是在处理这个问题,而不是我决定何时升级等:(

标签: mysql sql


【解决方案1】:

max 需要两次 row_number 拳头,然后 Mim 也需要两次,因此每个配置文件只能获得一行

CREATE TABLE tableA (
  `profile_id` INTEGER,
  `id` INTEGER,
  `created_at` datetime,
  tableA int
  
);

INSERT INTO tableA
  (`profile_id`, `id`, `created_at`)
VALUES
  ('1325586', '340807', '2021-08-05 21:30:55'),
  ('1325586', '340803', '2021-08-05 21:28:11'),
  ('1325586', '340805', '2021-08-05 21:28:11'),
  ('1325487', '340707', '2021-06-26 13:42:19'),
  ('1325487', '340453', '2021-03-25 10:49:40'),
  ('1325487', '340451', '2021-03-25 10:49:39');
CREATE TABLE tableB (p_id int,`type` VARCHAR(1))
INSERT INTO tableB VALUES(340807,'A'),(340803,'A'),(340805,'A'),(340707,'B'),(340453,'A'),(340451,'A')
WITH CTE AS(SELECT a.profile_id,a.id, a.created_at, b.type
FROM tableA a
JOIN tableB b ON a.id = b.p_id 
)
SELECT
`profile_id`, `id`, `created_at`, `type`
FROM
(SELECT 
t1.* , ROW_NUMBER() OVER( PARTITION  BY `profile_id` ORDER BY created_at ASC) rn
FROM CTE t1
 INNER JOIN (SELECT `profile_id`, `type`
FROM (
SELECT * , ROW_NUMBER() OVER( PARTITION  BY `profile_id` ORDER BY created_at DESC) rn FROM CTE) ta
WHERE rn = 1) t2 ON t1.`profile_id` =  t2.`profile_id` AND  t1.`type` = t2.`type`) tb 
WHERE rn = 1
profile_id |编号 | created_at |类型 ---------: | -----: | :----------------- | :--- 1325487 | 340707 | 2021-06-26 13:42:19 |乙 1325586 | 340803 | 2021-08-05 21:28:11 |一种

db小提琴here

对于 Mysql 5.7,这变得更丑了

CREATE TABLE tableA (
  `profile_id` INTEGER,
  `id` INTEGER,
  `created_at` datetime,
  tableA int
  
);

INSERT INTO tableA
  (`profile_id`, `id`, `created_at`)
VALUES
  ('1325586', '340807', '2021-08-05 21:30:55'),
  ('1325586', '340803', '2021-08-05 21:28:11'),
  ('1325586', '340805', '2021-08-05 21:28:11'),
  ('1325487', '340707', '2021-06-26 13:42:19'),
  ('1325487', '340453', '2021-03-25 10:49:40'),
  ('1325487', '340451', '2021-03-25 10:49:39');
CREATE TABLE tableB (p_id int,`type` VARCHAR(1))
INSERT INTO tableB VALUES(340807,'A'),(340803,'A'),(340805,'A'),(340707,'B'),(340453,'A'),(340451,'A')
SELECT
`profile_id`, `id`, `created_at`, `type`
FROM
(SELECT IF(a.profile_id = @profile, @rn := @rn +1, @rn:= 1) rn,a.id, a.created_at, b.type,
@profile := a.profile_id as profile_id
FROM tableA a
JOIN tableB b ON a.id = b.p_id
JOIN
(SELECT profile_id,type
FROM
(SELECT IF(a.profile_id = @profile, @rn := @rn +1, @rn:= 1) rn,a.id, a.created_at, b.type,
@profile := a.profile_id as profile_id
FROM tableA a
JOIN tableB b ON a.id = b.p_id , (SELECT @profile := 0, @rn := 0 ) t1
ORDEr BY a.profile_id,a.created_at DESC) ta
WHERE rn = 1) tb ON a.profile_id = tb.profile_id AND b.type = tb.type,(SELECT @profile := 0, @rn := 0 ) t1
ORDER BY a.profile_id,a.created_at ASC) tc
WHERE rn = 1
profile_id |编号 | created_at |类型 ---------: | -----: | :----------------- | :--- 1325586 | 340807 | 2021-08-05 21:30:55 |一种 1325487 | 340707 | 2021-06-26 13:42:19 |乙

db小提琴here

【讨论】:

  • 不幸的是,我看到我正在使用的 MySQL 服务器是 v5.7
  • 我加了一个5.7版本
  • 需要自己解决一些额外的条件,但这看起来是在正确的轨道上。谢谢
  • 让它按我的意愿工作,这正是我所需要的
猜你喜欢
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多