1

我有一个有两列startdateenddate(类型DATETIME)的表。当我传递开始日期和结束日期这两个参数时,我需要显示两列中两个日期之间的所有日期。

IE。我有这张桌子:

 startdate  enddate
 ---------------------
 6/1/2012   6/7/2012
 6/5/2012   6/9/2012
 6/10/2012  6/15/2012

当我传递两个日期时6/3/20126/20/2012我想显示这个结果集:

  6/5/2012
  6/7/2012
  6/9/2012
  6/10/2012
  6/15/2012

提前致谢

4

2 回答 2

1

UNION中的所有日期SubQuery。尝试,

SELECT allDates
FROM
(
    SELECT startdate as allDates
    FROM table
    UNION
    SELECT endDate as allDates
    FROM table
) a
WHERE allDates BETWEEN '6/3/2012' AND '6/20/2012'
于 2012-08-21T05:04:49.100 回答
0

MySql 示例语法:

CREATE TABLE TEST(STARTDATE DATE,ENDDATE DATE);

INSERT INTO TEST VALUES (NOW(), NOW());
INSERT INTO TEST VALUES ('2012-08-19', '2012-08-15');
INSERT INTO TEST VALUES ('2012-08-17', '2012-08-10');
INSERT INTO TEST VALUES ('2012-08-23', '2012-08-21');

SELECT STARTDATE FROM TEST WHERE STARTDATE BETWEEN '2012-08-15' AND '2012-08-23' 
UNION SELECT ENDDATE FROM TEST WHERE ENDDATE BETWEEN '2012-08-15' AND '2012-08-23' 
ORDER BY STARTDATE;
于 2012-08-21T05:17:54.297 回答