我有这样的日子列表
CREATE TABLE `date` (
`fecha` date NOT NULL,
`feriado` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcar la base de datos para la tabla `date`
--
INSERT INTO `date` VALUES ('2016-10-28', 0);
INSERT INTO `date` VALUES ('2016-10-29', 0);
INSERT INTO `date` VALUES ('2016-10-30', 1);
INSERT INTO `date` VALUES ('2016-10-31', 1);
INSERT INTO `date` VALUES ('2016-11-01', 0);
INSERT INTO `date` VALUES ('2016-11-02', 0);
INSERT INTO `date` VALUES ('2016-11-03', 0);
此查询添加日期
SELECT ADDDATE(CURDATE(),INTERVAL 2+(select COUNT(fecha) FROM date where fecha BETWEEN curdate() AND ADDDATE(CURDATE(),INTERVAL 2 DAY) AND feriado=1) DAY)
如果结果日期为feriado = 1,我怎么能这样做呢?它会在将来返回最接近feriado = 0的那个? 谢谢!
答案 0 :(得分:0)
如果你想在第二天用feriado = 0来获得第二天,那么另一种方法可能是排序和选择:
SELECT max(fecha) FROM (
SELECT fecha FROM date
WHERE feriado = 0
AND fecha > curdate()
ORDER by fecha
LIMIT 2
) AS foo;
答案 1 :(得分:0)
CREATE TABLE date (
fecha date NOT NULL PRIMARY KEY,
feriado tinyint NOT NULL
);
INSERT INTO `date` VALUES
('2016-10-28', 0),
('2016-10-29', 0),
('2016-10-30', 1),
('2016-10-31', 1),
('2016-11-01', 0),
('2016-11-02', 0),
('2016-11-03', 0);
SELECT x.*
, MIN(y.fecha) nearest
FROM date x
LEFT
JOIN date y
ON y.fecha >= x.fecha
AND y.feriado = 1
GROUP
BY x.fecha;
+------------+---------+------------+
| fecha | feriado | nearest |
+------------+---------+------------+
| 2016-10-28 | 0 | 2016-10-30 |
| 2016-10-29 | 0 | 2016-10-30 |
| 2016-10-30 | 1 | 2016-10-30 |
| 2016-10-31 | 1 | 2016-10-31 |
| 2016-11-01 | 0 | NULL |
| 2016-11-02 | 0 | NULL |
| 2016-11-03 | 0 | NULL |
+------------+---------+------------+