从今天起检索加入日期少于15天的行

时间:2017-11-07 10:17:16

标签: php mysql sql codeigniter mysqli

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天。我错过了什么。

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该将日期/时间存储为日期时间,而不是字符串。要使比较起作用,您需要将值转换为字符串:

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;