MySQL - 子查询 Returns 多于 1 行 - 减法问题
MySQL - Subquery Returns more than 1 row - subtraction issue
所以我有 2 个表,分别是预订和办公室
预订:
办公室:
请注意,我使用 BITWISE 运算来计算 30 分钟迭代中的可用时间。
现在的问题是,当用户需要预订新预订时,我需要从每个办公室的办公室可用性 (offices.mon_hours) 中减去预订时间 (sum(bookings.hours_booked)),这样我可以获得可用办公室的列表。
到目前为止,这是我的查询,但出现了这个错误:子查询 returns 多于 1 行
select * from offices o
where (mon_hrs - (SELECT SUM(hours_booked)
from bookings boo
where boo.week = 44
AND boo.year = 2020
AND CAST(from_timestamp as DATE) = '2020-10-26'
GROUP BY office_id)) & 4 ;
测试
SELECT offices.*
FROM offices
JOIN (SELECT SUM(hours_booked) summ, office_id
FROM bookings
WHERE week = 44
AND year = 2020
-- next replacement avoids table scan and allows index usage
-- AND CAST(from_timestamp as DATE) = '2020-10-26'
AND from_timestamp >= '2020-10-26'
AND from_timestamp < '2020-10-26' + INTERVAL 1 DAY
GROUP BY office_id) sums ON offices.id = sums.office_id
WHERE (offices.mon_hrs - sums.summ) & 4 ;
所以我有 2 个表,分别是预订和办公室
预订:
办公室:
请注意,我使用 BITWISE 运算来计算 30 分钟迭代中的可用时间。
现在的问题是,当用户需要预订新预订时,我需要从每个办公室的办公室可用性 (offices.mon_hours) 中减去预订时间 (sum(bookings.hours_booked)),这样我可以获得可用办公室的列表。
到目前为止,这是我的查询,但出现了这个错误:子查询 returns 多于 1 行
select * from offices o
where (mon_hrs - (SELECT SUM(hours_booked)
from bookings boo
where boo.week = 44
AND boo.year = 2020
AND CAST(from_timestamp as DATE) = '2020-10-26'
GROUP BY office_id)) & 4 ;
测试
SELECT offices.*
FROM offices
JOIN (SELECT SUM(hours_booked) summ, office_id
FROM bookings
WHERE week = 44
AND year = 2020
-- next replacement avoids table scan and allows index usage
-- AND CAST(from_timestamp as DATE) = '2020-10-26'
AND from_timestamp >= '2020-10-26'
AND from_timestamp < '2020-10-26' + INTERVAL 1 DAY
GROUP BY office_id) sums ON offices.id = sums.office_id
WHERE (offices.mon_hrs - sums.summ) & 4 ;