如何使用where子句获取正在搜索的同一列的值

时间:2015-12-05 15:15:03

标签: sql-server

------------------------------------------------
| UserId |       Checktime          | Checktype
|   1    | 12/05/2015 09:00:00:0000 | I
|   1    | 12/05/2015 18:00:00:0000 | O
|   1    | 12/04/2015 09:00:00:0000 | I
|   1    | 12/04/2015 18:00:00:0000 | O
|   2    | 12/05/2015 09:00:00:0000 | I
|   2    | 12/05/2015 18:00:00:0000 | O
|   2    | 12/04/2015 09:00:00:0000 | I
|   2    | 12/04/2015 18:00:00:0000 | O

我想只检索Checktime = UserId = 1date = 12/05/2015Checktype = {{1} I的时间在我的结果集中。

1 个答案:

答案 0 :(得分:0)

您选择查询以检索数据并过滤数据,其中日期大于2015年4月12日且小于13/04/2015且userId = 1.

所以你的查询会是这样的。

Mysl查询: -

SELECT DATE_FORMAT(Checktime,'%H:%i:%s') as TIME from TABLE_NAME where UserId=1 and Checktype='I' and Checktime>='12/04/2015' and Checktime <'13/04/2015'

编辑: - SQL Server 2008

SELECT CONVERT(TIME,Checktime) AS TIME from TABLE_NAME where UserId=1 and Checktype='I' and Checktime>='12/04/2015' and Checktime <'13/04/2015'