根据sql中的日期筛选数据

时间:2022-12-03 10:23:54

Im trying to execute the following SQL query and filter out the data based on the date.

我尝试执行下面的SQL查询并基于日期过滤数据。

I need to display a table which filters out the data such that, only those rows which are between the mentioned start_date and end_date

我需要显示一个表,它过滤掉数据,只显示在前面提到的start_date和end_date之间的那些行

Here's the query that I have been trying

这是我一直在尝试的查询

SELECT DISTINCT T1.column1, T1.column2, T2.START_DATE, T2.END_DATE
FROM Table1 T1, Table2 T2
WHERE (T1.column1= T2.column2) AND
(T2.START_DATE >= '15/01/2013 10:58:58' AND 
   T2.END_DATE <= '18/01/2013 10:58:58') ORDER BY T2.START_DATE DESC

I get the result with values from 2012 as well. Please help me out

我也得到了2012年的结果。请帮我出

Thanks

谢谢

1 个解决方案

#1


5  

Since you have not mentioned about any errors, if START_DATE and END_DATE are DATETIME data type, there is nothing wrong with your query. If you are not getting the correct records, Please check the data.

由于您没有提到任何错误,如果START_DATE和END_DATE是DATETIME数据类型,那么查询没有问题。如果您没有得到正确的记录,请检查数据。

However your date format may trouble you in a different server. There are some good practices you could adhere to avoid such issues.

但是,您的日期格式可能会影响您在其他服务器上的工作。有一些好的实践可以避免此类问题。

-Whenever date is used as a string, try to use it in ISO or ISO8601 format (ie 'yyyymmdd' or 'yyyy-mm-ddThh:mi:ss.mmm')

-当日期被用作字符串时,尝试使用ISO或ISO8601格式(即“yyyyyyymmdd”或“yyyyyyyy -mm- ddthh:mi:ss.mmm”)

-Also avoid joining tables with WHERE Table1, Table2 which is old and obsolete. JOINs are much better performed, neat and tidy.

-也要避免与表1、表2中陈旧过时的表连接。连接执行得更好,更整洁。

You can change your query as follows;

您可以按照以下方式更改查询;

SELECT DISTINCT T1.column1, T1.column2, T2.START_DATE, T2.END_DATE
FROM Table1 T1 JOIN Table2 T2 ON T1.column1 = T2.column2
WHERE (T2.START_DATE >= '20130115 10:58:58' AND 
       T2.END_DATE <= '20130118 10:58:58') 
ORDER BY T2.START_DATE DESC

#1


5  

Since you have not mentioned about any errors, if START_DATE and END_DATE are DATETIME data type, there is nothing wrong with your query. If you are not getting the correct records, Please check the data.

由于您没有提到任何错误,如果START_DATE和END_DATE是DATETIME数据类型,那么查询没有问题。如果您没有得到正确的记录,请检查数据。

However your date format may trouble you in a different server. There are some good practices you could adhere to avoid such issues.

但是,您的日期格式可能会影响您在其他服务器上的工作。有一些好的实践可以避免此类问题。

-Whenever date is used as a string, try to use it in ISO or ISO8601 format (ie 'yyyymmdd' or 'yyyy-mm-ddThh:mi:ss.mmm')

-当日期被用作字符串时,尝试使用ISO或ISO8601格式(即“yyyyyyymmdd”或“yyyyyyyy -mm- ddthh:mi:ss.mmm”)

-Also avoid joining tables with WHERE Table1, Table2 which is old and obsolete. JOINs are much better performed, neat and tidy.

-也要避免与表1、表2中陈旧过时的表连接。连接执行得更好,更整洁。

You can change your query as follows;

您可以按照以下方式更改查询;

SELECT DISTINCT T1.column1, T1.column2, T2.START_DATE, T2.END_DATE
FROM Table1 T1 JOIN Table2 T2 ON T1.column1 = T2.column2
WHERE (T2.START_DATE >= '20130115 10:58:58' AND 
       T2.END_DATE <= '20130118 10:58:58') 
ORDER BY T2.START_DATE DESC