我有一个查询。在过去3天(工作日)中,我想列出没有数据的那些。
服务表:records1:
--------------------- ---------------------------------------
| id | name | | id | service_id | date |
|-------------------| |-------------------------------------|
| 1 | a | | 1 | 1 | 2019-03-26 |
| 2 | ab | | 2 | 3 | 2019-03-25 |
| 3 | ac | | 3 | 6 | 2019-03-24 | (weekend)
| 4 | ad | | 4 | 4 | 2019-03-26 |
| 5 | af | ---------------------------------------
| 6 | ah |
---------------------
Select s.id, s.name from service s
where s.id not in (select service_id from records1 where date(dateff) > DATE_SUB(CURDATE(), INTERVAL 3 DAY) and WEEKDAY(curdate()) in (0,1,2,3,4) )
and WEEKDAY(curdate()) in (0,1,2,3,4)
我需要:例如今天是星期二(2019-03-26)。该表将列出表中最近3天(周二,周一,周五)没有数据的名称。
表格:
---------------------
| name | name |
|-------------------|
| 1 | a |
| 3 | ac |
| 4 | ad |
---------------------
答案 0 :(得分:0)
https://www.db-fiddle.com/f/bwobAMFSchEDq6AxbHaL7/0
SELECT
s.id,
s.name,
COUNT(DISTINCT r.id)
FROM service s
LEFT JOIN records1 r
ON date(r.dateff) >= @today - INTERVAL @period DAY
AND r.service_id = s.id
AND WEEKDAY(r.dateff) in (0,1,2,3,4)
CROSS JOIN (
SELECT
@today := DATE('2019-03-26'),
@weekDay := WEEKDAY(@today),
@period := IF(@weekDay IN (2,3,4),3, IF(@weekDay IN (0,1,6), 5, 4))
) vars
GROUP BY s.id, s.name
HAVING COUNT(DISTINCT r.id) = 0