我正在尝试根据日期和月份创建一个查询,同时在一段时间内的两列之间的条件如下:
以下是demo
以下是信息:
CREATE TABLE clients
(date_birth date, [date_anniversary] date)
;
INSERT INTO clients
([date_birth], [date_anniversary])
VALUES
('1911-01-04',NULL ),('1921-01-05',NULL ),('1931-01-06',NULL ),('1941-01-07',NULL ),
('1951-01-08',NULL ),('1961-01-09',NULL ),('1971-01-10',NULL ),('1981-01-11',NULL ),
('1991-01-12',NULL ),(NULL, '1998-02-13'),(NULL, '1999-02-14'),(NULL, '2000-02-15'),
(NULL, '2001-02-16'),(NULL, '2002-02-17'),(NULL, '2003-02-18'),(NULL, '2004-02-19'),
(NULL, '2005-02-20'),(NULL, '2006-02-21');
以下是条件:
since "04-01" until "13-02"
show all from clients where date_birth BETWEEN "04-01" AND "13-02" OR date_anniversary "04-01" AND "13-02"
这应该是输出:
('1911-01-04',NULL ),
('1921-01-05',NULL ),
('1931-01-06',NULL ),
('1941-01-07',NULL ),
('1951-01-08',NULL ),
('1961-01-09',NULL ),
('1971-01-10',NULL ),
('1981-01-11',NULL ),
('1991-01-12',NULL ),
(NULL, '1998-02-13'),
我试过这个,但仅仅是一个月:
select date_birth from clients
where month(date_birth) BETWEEN '00' AND '01'
还尝试过:
SET @date1 := 01-14;
SET @date2 := 02-20;
select date_birth from clients
where date_birth BETWEEN @date1 AND @date2
请有人帮助我吗?
答案 0 :(得分:1)
您可以使用Mysql的月日函数
来实现SELECT date_birth ,date_anniversary
FROM clients
WHERE
(
MONTH(date_birth) >= 01 AND MONTH(date_birth) <= 01
AND DAY(date_birth) >= 01 AND DAY(date_birth) <= 31
)
OR
(
MONTH(date_anniversary) >= 01 AND MONTH(date_anniversary) <= 01
AND DAY(date_anniversary) >= 01 AND DAY(date_anniversary) <= 31
)
使用Mysql的date_format函数只需按天和月对每列格式进行比较即可与提供的相同格式范围进行比较,同时提供您的参数,如'%m-%d'格式
select date_birth ,date_anniversary
from clients
where date_format(date_birth,'%m-%d') BETWEEN '01-01' AND '01-31'
or
date_format(date_anniversary,'%m-%d') BETWEEN '01-01' AND '01-31'
将值存储在变量中
SET @start:='01-01';
SET @end:='01-31';
SELECT date_birth ,date_anniversary
FROM clients
WHERE DATE_FORMAT(date_birth,'%m-%d') BETWEEN @start AND @end
OR
DATE_FORMAT(date_anniversary,'%m-%d') BETWEEN @start AND @end
答案 1 :(得分:0)
如果您使用的是MySQL,请尝试:
SELECT
date_birth
,date_anniversary
FROM clients
WHERE
DATE_FORMAT(date_birth, '%m-%d') BETWEEN DATE_FORMAT('1900-01-04', '%m-%d') AND DATE_FORMAT('1900-02-13', '%m-%d')
OR DATE_FORMAT(date_anniversary, '%m-%d') BETWEEN DATE_FORMAT('1900-01-04', '%m-%d') AND DATE_FORMAT('1900-02-13', '%m-%d')
如果您使用MSSQL e发布到错误的主题,请尝试使用等效的DATE_FORMAT()函数。