簡體   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

MySQL表EMPLOYEE具有類型為int的列(beginyear,beginmonth,beginday,empid)。

返回等於或大於日期2009/8/13的所有行的正確查詢是什么? 那是年,月,日。

這樣的查詢是不正確的,因為它不會返回包含日期的行,例如2009/9/1(在下面的where子句中由beginday> = 13過濾掉)或2010/1/14。

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

假設我無法對架構進行任何更改,並且必須從JDBC創建某種查詢才能獲得結果。

對於您在三個不同領域的糟糕情況,我能做的最好的事情:

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

MySql的datetime表達式概念很特殊,您可能想用condate date()函數包裝concat以對其進行規范化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM