获取具有相同Id但在两个给定日期之间的记录

时间:2017-05-22 11:41:51

标签: sql sql-server-2012

我有一张如下表:

MyTable的

FileNo: |  amount | date
---------------------------------------------------
111     | 55      | 2017-04-07 04:51:45.870
111     | 55      | 2017-04-08 04:51:45.870
111     | 55      | 2017-04-09 04:51:45.870
111     | 55      | 2017-04-10 04:51:45.870
999     | 55      | 2017-04-11 04:51:45.870
111     | 55      | 2017-04-12 04:51:45.870
999     | 55      | 2017-04-13 04:51:45.870
111     | 55      | 2017-04-14 04:51:45.870
999     | 55      | 2017-04-15 04:51:45.870

现在我想要获取FileNo:是' 111'和日期在' 04/07/2017'和' 04/10 / 2017'

我写了如下的查询: -

select * from MyTable where FileNo: = 111 and date >= '04/07/2017' and date <= '04/10/2017' 

但它没有工作,没有发现错误,它没有返回任何记录,它只是显示空表!,我的语法错误或我错过了&#39; Group By&# 39;或任何其他条款?

3 个答案:

答案 0 :(得分:1)

如果是MSSQL Server,请尝试

SELECT * FROM MyTable 
WHERE FileNo: = 111
  AND CAST(date AS DATE) BETWEEN '2017-07-04' AND '2017-10-04'

答案 1 :(得分:0)

您的日期文字格式错误。请尝试使用ANSI标准names_list = [] # there are more than one name in the list for _ in range(5): # the loop index is not needed, so I use the anonymous underscore name = raw_input("Enter the student's name: ") names_list.append(name)

yyyy-MM-dd

答案 2 :(得分:0)

select * from MyTable where FileNo: = 111 and date >= '04/07/2017' and date <= '04/10/2017'

此查询是正确的,因此您还没有看到任何错误。但是,不会匹配任何日期。

请注意:

  • 2017-04-07 04:51:45.870 = 07 April 2017;和
  • 04/10/2017 = 04 October 2017

所以你永远不会期望在那里看到任何结果。

解决方案

select * from MyTable where FileNo: = 111 and date >= '2017-04-07' and date <= '2017-04-15'

同时删除仅使用日期的时间,即yyyy-MM-dd NOT yyyy-MM-dd hhmmss