簡體   English   中英

從今天起檢索加入日期少於 15 天的行

[英]Retrieve rows with joining date less than 15 days from today

Mysql查詢是

SELECT 
    userid as uid, 
    CONCAT( firstname, ' ', lastname ) AS name,
    profileimage,
    joining_date, 
    role as designation
FROM pr_users_details
INNER JOIN pr_users ON pr_users.id = pr_users_details.userid
LEFT JOIN pr_userroles ON pr_userroles.id = pr_users.userroleid
WHERE joining_date > DATE_SUB( NOW()-1 , INTERVAL 15 DAY )
ORDER BY pr_users_details.id DESC

我得到了這些一年前的結果。 我想顯示新員工,即今天的日期 - 15 天。 我是不是錯過了什么。

在此處輸入圖片說明

您應該將日期/時間存儲為日期時間,而不是字符串。 為了進行比較,您需要將值轉換為字符串:

WHERE STR_TO_DATE(joining_date, '%d-%m-%Y') > DATE_SUB( NOW()-1 , INTERVAL 15 DAY )

但是,您應該修復數據:

update ?  -- whatever the table is
    set joining_date = STR_TO_DATE(joining_date, '%d-%m-%Y');  -- this will convert the date to a canonical format

alter table t alter column joining_date date;

暫無
暫無

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

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