【发布时间】:2019-09-30 16:00:00
【问题描述】:
想象一个工人被允许在定义的时间段内进行可变数量的轮班。我们希望查询任何时期内超过允许数量的班次以及所有时期外的班次。
我已将 db-fiddle 与测试查询相关联。这里的问题是:
- 对于多余的班次,合并班次的顺序是不确定的。我只想看到多余的班次(2016-05-30 是在仅授权 3 个班次的时期内的第 4 个班次)。
- 我也想看看根本没有授权的3班(2019-04-25、2019-06-02、2019-06-04)。
我希望我需要翻转查询(即从 Shift join Authorization 中选择)并使用 group by、order by 和 limit 的某种组合,但我没有取得任何成功。任何意见将不胜感激。
CREATE TABLE `Authorization` (
`AuthorizationId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`WorkerId` int(10) unsigned NOT NULL,
`Start` date NOT NULL,
`End` date NOT NULL,
`ShiftsAllowed` int(10) unsigned NOT NULL,
PRIMARY KEY (`AuthorizationId`)
);
CREATE TABLE `Shift` (
`ShiftId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`WorkerId` int(10) unsigned NOT NULL,
`Date_` date NOT NULL,
PRIMARY KEY (`ShiftId`)
);
INSERT INTO Authorization (WorkerId,Start,End,ShiftsAllowed) VALUES
(1,'2019-05-01','2019-05-15',2),
(1,'2019-05-16','2019-05-31',3);
INSERT INTO Shift (WorkerId,Date_) VALUES
(1,'2019-04-25'),
(1,'2019-05-01'),
(1,'2019-05-10'),
(1,'2019-05-16'),
(1,'2019-05-20'),
(1,'2019-05-25'),
(1,'2019-05-30'),
(1,'2019-06-02'),
(1,'2019-06-04');
select
Authorization.Start,
Authorization.End,
Authorization.ShiftsAllowed,
count(Shift.Date_),
group_concat(Shift.Date_),
reverse(
substring_index(
reverse(group_concat(Shift.Date_)),
',',
count(Shift.Date_) - Authorization.ShiftsAllowed
)
)
from Authorization
left join Shift
on
Shift.WorkerId = Authorization.WorkerId
and Shift.Date_ between Authorization.Start and Authorization.End
group by Authorization.AuthorizationId
having
count(Shift.ShiftId) > Authorization.ShiftsAllowed
【问题讨论】:
-
学会正确使用
GROUP BY。 -
@Eric 你介意描述或展示一个例子吗?
-
Authorization.Start、Authorization.End、Authorization.ShiftsAllowed都是非聚合列,必须包含在GROUP BY中。没有其他 dbms 系统将运行您的代码。即使是较新版本的MySQL也不会运行您的代码。
标签: mysql sql join group-by count