我已经阅读了MySQL order by before group by上的答案,但是将它应用于我的查询最终会在子查询中找到一个子查询,这是一个相当简单的案例,所以我想知道这是否可以简化:
为简洁起见,我省略了members
表上的其他字段。此外,在实际应用程序中还有更多表加入,但这些表很容易加入。这是membership_stack
表给我提出的问题。
CREATE TABLE members (
id int unsigned auto_increment,
first_name varchar(255) not null,
PRIMARY KEY(id)
);
INSERT INTO members (id, first_name)
VALUES (1, 'Tyler'),
(2, 'Marissa'),
(3, 'Alex'),
(4, 'Parker');
CREATE TABLE membership_stack (
id int unsigned auto_increment,
member_id int unsigned not null,
sequence int unsigned not null,
team varchar(255) not null,
`status` varchar(255) not null,
PRIMARY KEY(id),
FOREIGN KEY(member_id) REFERENCES members(id)
);
-- Algorithm to determine correct team:
-- 1. Only consider rows with the highest sequence number
-- 2. Order statuses and pick the first one found:
-- (active, completed, cancelled, abandoned)
INSERT INTO membership_stack (member_id, sequence, team, status)
VALUES (1, 1, 'instinct', 'active'),
(1, 1, 'valor', 'abandoned'),
(2, 1, 'valor', 'active'),
(2, 2, 'mystic', 'abandoned'),
(2, 2, 'valor', 'completed'),
(3, 1, 'instinct', 'completed'),
(3, 2, 'valor', 'active');
我无法更改数据库架构,因为数据与外部数据源同步。
这是我到目前为止所做的:
SELECT m.id, m.first_name, ms.sequence, ms.team, ms.status
FROM membership_stack AS ms
JOIN (
SELECT member_id, MAX(sequence) AS sequence
FROM membership_stack
GROUP BY member_id
) AS t1
ON ms.member_id = t1.member_id
AND ms.sequence = t1.sequence
RIGHT JOIN members AS m
ON ms.member_id = m.id
ORDER BY m.id, FIELD(ms.status, 'active', 'completed', 'cancelled', 'abandoned');
这可以按预期工作,但如果“最近的序列”涉及多个团队,则成员可能会多次出现。我需要做的是在id
上再次聚合并选择每组中的第一行。
然而,这会带来一些问题:
FIRST()
function in MySQL FIELD(ms.status, 'active', 'completed', 'cancelled', 'abandoned')
被认为是此结果集上的功能相关字段。该查询还需要与MySQL 5.1兼容,因为这是我们目前正在运行的。| id | first_name | sequence | team | status |
|----|------------|----------|----------|-----------|
| 1 | Tyler | 1 | instinct | active |
| 2 | Marissa | 2 | valor | completed |
| 3 | Alex | 2 | valor | active |
| 4 | Parker | NULL | NULL | NULL |
我该怎么办?
编辑:我注意到有些成员不属于任何团队。这些成员应包含在结果集中,并为这些字段使用空值。更新问题以反映新信息。
答案 0 :(得分:0)
我会使用变量来做到这一点。
您正在寻找最适合您特殊订购的membership_stack
行。我只关注那个。 join
返回members
是微不足道的。
select ms.*
from (select ms.*,
(@rn := if(@m = member_id, @rn + 1,
if(@m := member_id, 1, 1)
)
) as rn
from membership_stack ms cross join
(select @m := -1, @rn := 0) params
order by member_id, sequence desc,
field(ms.status, 'active', 'completed', 'cancelled', 'abandoned')
) ms
where rn = 1;
变量是逻辑的实现方式。排序是获得正确结果的关键。
编辑:
MySQL对子查询中的LIMIT
非常挑剔。这可能会起作用:
select ms.*
from membership_stack ms
where (sequence, status) = (select ms2.sequence, ms2.status
from membership_stack ms2
where ms2.member_id = ms.member_id
order by ms2.member_id, ms2.sequence desc,
field(ms2.status, 'active', 'completed', 'cancelled', 'abandoned')
limit 1
);
答案 1 :(得分:0)
您可以在WHERE子句中使用LIMIT 1:
中的相关子查询SELECT m.id, m.first_name, ms.sequence, ms.team, ms.status
FROM members AS m
JOIN membership_stack AS ms ON ms.member_id = m.id
WHERE ms.id = (
SELECT ms1.id
FROM membership_stack AS ms1
WHERE ms1.member_id = ms.member_id
ORDER BY ms1.sequence desc,
FIELD(ms1.status, 'active', 'completed', 'cancelled', 'abandoned'),
ms1.id asc
LIMIT 1
)
ORDER BY m.id;
演示:http://rextester.com/HGU18448
<强>更新强>
要包含membership_stack
表中没有条目的成员,您应该使用LEFT JOIN,并将子查询条件从WHERE子句移动到ON子句:
SELECT m.id, m.first_name, ms.sequence, ms.team, ms.status
FROM members AS m
LEFT JOIN membership_stack AS ms
ON ms.member_id = m.id
AND ms.id = (
SELECT ms1.id
FROM membership_stack AS ms1
WHERE ms1.member_id = ms.member_id
ORDER BY ms1.sequence desc,
FIELD(ms1.status, 'active', 'completed', 'cancelled', 'abandoned'),
ms1.id asc
LIMIT 1
)
ORDER BY m.id;