我需要在MySQL数据库中查找介于“YYYY-10-30”和“YYYY-12-11”之间的所有行。我不关心日期是哪一年介于两者之间日期。
答案 0 :(得分:5)
SELECT *
FROM yourTable
WHERE (MONTH(yourDate) = 10 AND DAYOFMONTH(yourDate) >= 30)
OR (MONTH(yourDate) = 11)
OR (MONTH(yourDate) = 12 AND DAYOFMONTH(yourDate) <= 11)
答案 1 :(得分:1)
select *
from table
where
concat(year(now()),right(date_field,6))
between
concat(year(now()),'-10-30')
and
concat(year(now()),'-12-11')
答案 2 :(得分:1)
你可以尝试一下吗?:
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetByMonthAndDayRange`
(
IN monthFrom INT
, IN dayFrom INT
, IN monthTo INT
, IN dayTo INT
)
BEGIN
-- For good coding practice, name your columns instead of using *
SELECT *
FROM tablename
WHERE
(MONTH(datecolumnname) = monthFrom AND DAYOFMONTH(datecolumnname) >= dayFrom)
OR (MONTH(datecolumnname) > monthFrom AND MONTH(datecolumnname) < monthTo)
OR (MONTH(datecolumnname) = monthTo AND DAYOFMONTH(datecolumnname) <= dayTo);
END
如果您使用存储过程(如果代码符合您的口味),您将能够更快地编码,只需提供参数(这是动态的)