简体   繁体   English

MySQL查询返回等于或大于特定日期的行,其中日期以年,月和日列分隔

[英]MySQL query to return rows that are equal to or greater than a certain date, where the date is separated in year, month and day columns

A MySQL table EMPLOYEE has columns (beginyear, beginmonth, beginday, empid) all of type int. MySQL表EMPLOYEE具有类型为int的列(beginyear,beginmonth,beginday,empid)。

What's a correct query to return all rows that are equal to or greater than the date 2009/8/13? 返回等于或大于日期2009/8/13的所有行的正确查询是什么? That's year, month, day. 那是年,月,日。

A query such as this is incorrect because it wouldn't return rows that contained dates such as 2009/9/1 (filtered out by beginday >=13 in where clause below) or 2010/1/14. 这样的查询是不正确的,因为它不会返回包含日期的行,例如2009/9/1(在下面的where子句中由beginday> = 13过滤掉)或2010/1/14。

SELECT *
FROM EMPLOYEE
where beginyear >= 2009
  and beginmonth >= 8
  and beginday >=13

Assume I can't make any changes to the schema and that I have to create some sort of query from JDBC to get the results. 假设我无法对架构进行任何更改,并且必须从JDBC创建某种查询才能获得结果。

The best I could do with your bad situation of three different fields: 对于您在三个不同领域的糟糕情况,我能做的最好的事情:

select *, concat(beginyear, '-',beginmonth,'-',beingday) as full_date 
  FROM TABLE 
   WHERE CONCAT(beginyear, '-',beginmonth,'-',beingday) >= '2009-08-13'

MySql's notion of a datetime expression is sort of peculiar, you might want to wrap the concat with a date() function to normalize it. MySql的datetime表达式概念很特殊,您可能想用condate date()函数包装concat以对其进行规范化。

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

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