开发者

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.

What's a correct query to return all rows that are equal to or greater than the date 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.

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 r开发者_运维问答esults.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜