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

Get records between two date range in MYSQL

我的 mysql 数据库中有一个 table。这是我的 table.

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)之间获得 material

这是我的查询...

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

但问题是没有给出输出。

我想你想要这个,你只是弄错了专栏。

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 |