获取MYSQL中两个日期范围之间的记录

时间:2016-05-24 05:47:56

标签: mysql date between

我的mysql数据库中有一个表。这是我的桌子。

material       sorg      f_date       t_date
2000012        2100    2016-05-01   2016-05-30
2000013        2100    2016-05-01   2016-05-21
2000021        2200    2016-05-01   2016-05-30
2000151        2100    2016-05-01   2016-05-15
2000336        2300    2016-05-01   2016-05-04
2000366        2300    2016-05-01   2016-05-24
2000451        2400    2016-05-01   2016-05-30
2000493        2200    2016-05-01   2016-05-11

我希望在两个给定日期(f_date和t_date)之间获得材料

这是我的查询...

SELECT tbl_fast_goods.material from tbl_fast_goods WHERE f_date >= '2016-05-23' AND t_date <= '2016-05-29'

输出应为。

2000012, 2000021, 2000366, 2000451

但问题是没有给出输出。

1 个答案:

答案 0 :(得分:1)

我想你想要这个,你只是误解了专栏。

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE tbl
    (`id` int, `user_id` int, `weather_type` varchar(5))
;

INSERT INTO tbl
    (`id`, `user_id`, `weather_type`)
VALUES
    (1, 12, 'cloud'),
    (2, 12, 'rain'),
    (3, 12, 'clear'),
    (4, 14, 'rain'),
    (5, 15, 'clear')
;


CREATE TABLE tbl_fast_goods 
    (`material` int, `sorg` int, `f_date` datetime, `t_date` datetime)
;

INSERT INTO tbl_fast_goods 
    (`material`, `sorg`, `f_date`, `t_date`)
VALUES
    (2000012, 2100, '2016-05-01 00:00:00', '2016-05-30 00:00:00'),
    (2000013, 2100, '2016-05-01 00:00:00', '2016-05-21 00:00:00'),
    (2000021, 2200, '2016-05-01 00:00:00', '2016-05-30 00:00:00'),
    (2000151, 2100, '2016-05-01 00:00:00', '2016-05-15 00:00:00'),
    (2000336, 2300, '2016-05-01 00:00:00', '2016-05-04 00:00:00'),
    (2000366, 2300, '2016-05-01 00:00:00', '2016-05-24 00:00:00'),
    (2000451, 2400, '2016-05-01 00:00:00', '2016-05-30 00:00:00'),
    (2000493, 2200, '2016-05-01 00:00:00', '2016-05-11 00:00:00')
;

查询1

SELECT tbl_fast_goods.material
from tbl_fast_goods
WHERE t_date >= '2016-05-23' AND f_date <= '2016-05-29'

<强> Results

| material |
|----------|
|  2000012 |
|  2000021 |
|  2000366 |
|  2000451 |