简体   繁体   English

PHP,MySQL选择当前年份,月份范围内的记录

[英]Php, MySql select records within current year, month range

I'm trying to create a query to select records that fit current year month range. 我正在尝试创建一个查询以选择适合当前年份月份范围的记录。 Here is my database table: 这是我的数据库表:

id      name        date_start  date_end
======================================
1       John        2016-05-20  2018-01-01
2       Peter       2017-02-02  2017-05-10
3       Mike        2015-02-02  2017-02-06

and my query: 和我的查询:

SELECT  * FROM users 
WHERE YEAR(date_start) = 2017 
  AND MONTH(date_start) = 01 
  OR YEAR(date_end) = 2017 
  AND MONTH(date_end) = 01 

So basically I need to select all users where current year/month in this case 2017/01 is within start/end column range. 因此,基本上,我需要选择在这种情况下,当前年度/月份在开始/结束列范围内的所有用户。

In above query, only John and Mike records should be selected. 在上面的查询中,仅应选择John和Mike记录。

I think you just want to find out which users have the date 2017-01-01 within their ranges, defined by the date_start and date_end . 我认为您只想找出日期范围内由date_startdate_end定义的用户2017-01-01 If so, then the following query should work: 如果是这样,则以下查询应该工作:

SELECT *
FROM users
WHERE date_start <= '2017-01-01' AND date_end >= '2017-01-01'

Demo here: 演示在这里:

SQLFiddle SQLFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM