仅使用TIMESTAMP SQL中的日期

时间:2018-07-31 05:12:04

标签: mysql sql

我使用选择命令来获取“ created_at”。这是创建行的日期。命令的结果是2018-07-29 15:29:57。我如何只显示日期并删除时间。

当前结果::2018-07-29 15:29:57

所需结果: 2018-07-29

SELECT treatment_log.created_at
FROM treatment_log
LEFT JOIN customers
    ON treatment_log.treatment_fk = customers.id
WHERE treatment_log.created_at = 
        (
            SELECT MAX(created_at)
            FROM treatment_log
            WHERE treatment_fk = ?
        )
    AND customers.id = ?

如何更改当前代码?

3 个答案:

答案 0 :(得分:0)

just.use日期功能

select date(now())将返回今天的日期而不是时间

在子查询1中,首先将日期进行转换,然后进行最大值选择max(日期(created_at))

答案 1 :(得分:0)

您可以简单地尝试一下

    SELECT SUBSTRING(MAX(created_at),0,10)
    FROM treatment_log
    WHERE treatment_fk = ?

    SELECT MAX(DATE_FORMAT(created_at,'yyyy-mm-dd'))
    FROM treatment_log
    WHERE treatment_fk = ?

答案 2 :(得分:0)

嗨,下面是解决方法

Sql Server:

SELECT CAST(treatment_log.created_at as DATE)
FROM treatment_log
LEFT JOIN customers
    ON treatment_log.treatment_fk = customers.id
WHERE treatment_log.created_at = 
        (
            SELECT MAX(created_at)
            FROM treatment_log
            WHERE treatment_fk = ?
        )
    AND customers.id = ?

对于我的SQL

SELECT DATE(treatment_log.created_at)
FROM treatment_log
LEFT JOIN customers
    ON treatment_log.treatment_fk = customers.id
WHERE treatment_log.created_at = 
        (
            SELECT MAX(created_at)
            FROM treatment_log
            WHERE treatment_fk = ?
        )
    AND customers.id = ?