SQL查询从datetime选择日期等于行

时间:2016-05-30 15:48:46

标签: mysql sql

我需要在日期等于特定日期时选择行,其中TIMESTAMP列包含日期时间。

这很好用,

select USERNAME from ACCESSACTIVITY where UPPER(LOCATION)=UPPER('remote-PC')and TIMESTAMP = STR_TO_DATE('28-05-2016 01:00:00','%d-%m-%Y %H:%i:%s')

当我避开时间时,它不起作用。

select USERNAME from ACCESSACTIVITY where UPPER(LOCATION)=UPPER('remote-PC')and TIMESTAMP = STR_TO_DATE('28-05-2016 ','%d-%m-%Y')

3 个答案:

答案 0 :(得分:2)

使用:

select USERNAME from ACCESSACTIVITY where UPPER(LOCATION)=UPPER('remote-PC')and TIMESTAMP LIKE STR_TO_DATE('28-05-2016 ','%d-%m-%Y')+'%'

答案 1 :(得分:1)

您不需要通配符。 试试这个:

select USERNAME from ACCESSACTIVITY where UPPER(LOCATION)=UPPER('remote-PC')and DATE(TIMESTAMP) = STR_TO_DATE('28-05-2016 ','%d-%m-%Y')

答案 2 :(得分:0)

假设timestamp存储为datetime,请使用不等式:

select USERNAME
from ACCESSACTIVITY
where UPPER(LOCATION)=UPPER('remote-PC') and
     TIMESTAMP >= DATE('2016-05-28') AND
     TIMESTAMP < DATE('2016-05-29');

如果有适当的索引,这允许数据库引擎在TIMESTAMP上使用索引。