简体   繁体   English

SQL查询在特定日期无效

[英]SQL query doesn't work on a specific day

I have a weird problem in mysql! 我在mysql中有一个奇怪的问题! my query is 我的查询是

SELECT * FROM aa WHERE problemTime>= '2016/03/20' AND problemTime<= '2016/04/20' 

the result of this query is nothing , but when I change the first time to 2016/03/19 or 2016/03/21 I have the following result! 该查询的结果为空,但是当我第一次更改为2016/03/19或2016/03/21时,我得到以下结果! I mean these queries 我的意思是这些查询

SELECT * FROM aa WHERE problemTime>= '2016/03/21' AND problemTime<= '2016/04/20' 

or 要么

SELECT * FROM aa WHERE problemTime>= '2016/03/19' AND problemTime<= '2016/04/20' 

the result in both time ( 19th and 21th) is 两个时间(第19和21)的结果均为

在此处输入图片说明

but when I use 20th the result is noting 但是当我使用20th时,结果是

my main table is 我的主表是

在此处输入图片说明

I change the format of time from 2016/03/20 to 2016-03-20 ( I mean change / to - ) but it doesn't have change too! 我将时间格式从2016/03/20更改为2016-03-20(我的意思是将更改/更改为-),但它也没有更改!

whats the problem? 有什么问题?

You should really be running a query like this if your problemTime column is datetime type: 如果您的problemTime列是datetime类型,则您实际上应该运行这样的查询:

SELECT * FROM aa 
WHERE    
  problemTime>= str_to_date('2016/03/20', '%Y/%m/%d') AND 
  problemTime <= str_to_date('2016/04/20', '%Y/%m/%d')

Don't rely on implicit conversions between string and date.. leave your table data alone and ensure you explicitly convert your where clause parameters to the same data type as in the table. 不要依赖于字符串和日期之间的隐式转换。请不要使用表数据,并确保将where子句参数显式转换为与表中相同的数据类型。 Also remember that a date "without" a time is actually midnight on the day in question, and midnight is like zero, it's the first thing that happens on any given day. 还请记住,“没有”时间的日期实际上是相关日期的午夜,而午夜就像零,这是在任何给定日期发生的第一件事。 A time of 6am on a given date, is after midnight, so a query that asks for dates less than or equal to midnight on a particular date means the 6am date will be excluded 给定日期的凌晨6点在午夜之后,因此查询要求在特定日期小于或等于午夜的日期意味着将排除凌晨6点的日期

This is general good DB practice; 这是一般的良好数据库实践; do not convert table data where possible, because it can cause huge performance hits and wrong results 请勿在可能的情况下转换表数据,因为这可能会导致巨大的性能损失和错误的结果

Your column "problemTime" have date with time. 您的列“ problemTime”具有日期和时间。 Do not convert table data, change your where clause (add time). 不要转换表数据,请更改where子句(添加时间)。

SELECT * FROM aa WHERE problemTime>= '2016/03/20 00:00:00' AND problemTime<= '2016/04/20 23:59:59'

Try this as per SQl Server. 按照SQl服务器尝试此操作。

 SELECT * FROM aa 
 WHERe  cast(problemTime AS date)  between  '2016/03/21' AND '2016/04/20' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM