【问题标题】:Retrieve rows with joining date less than 15 days from today从今天起检索加入日期少于 15 天的行
【发布时间】:2017-11-07 10:17:16
【问题描述】:

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

【问题讨论】:

标签: mysql sql


【解决方案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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 2011-09-26
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多