MySQL使用BETWEEN检索数据

时间:2013-06-17 04:34:06

标签: php mysql

我有一个看起来像下面名为dtr的表。 我在使用BETWEEN查询时遇到问题。 喜欢:

$between = mysql_query("Select * from dtr where State Between 'Check In' And 'Check Out'");

我想要它做的是在Check in和Check Out中的BETWEEN中获得STATE Out and Out Back的行。但它似乎无法奏效。我还应该用什么?或者呢? 因为所有我得到它签入和签出结果。

   Name      | AC-No.|    Date   |    Time      |    State   | Exception| Operation
-------------+-------+-----------+--------------+------------+----------+----------
Johnny Starks| 1220  | 5/13/2013 | 11:45:18 PM  |  Check In  |   OK     |
Johnny Starks| 1220  | 5/14/2013 | 12:46:58 AM  |  Out       |   Out    |
Johnny Starks| 1220  | 5/14/2013 | 12:52:41 AM  |  Out Back  |   Out    |
Johnny Starks| 1220  | 5/14/2013 | 02:12:50 AM  |  Out       |   Out    |
Johnny Starks| 1220  | 5/14/2013 | 02:43:11 AM  |  Out Back  |   Out    |
Johnny Starks| 1220  | 5/14/2013 | 05:46:58 AM  |  Out       |   Out    |
Johnny Starks| 1220  | 5/14/2013 | 06:22:41 AM  |  Out Back  |   Out    |
Johnny Starks| 1220  | 5/14/2013 | 16:55:12 AM  |  Check Out |   OK     |

2 个答案:

答案 0 :(得分:2)

将查询与IN

一起使用
Select * from dtr 
where State IN('Check In','Check Out','Out','Out Back')

BETWEEN主要用于datenumeric数据。

答案 1 :(得分:2)

如果符合以下条件,您只能对范围使用字符串比较:

'Check In' < 'Out' < 'Out Back' < 'Check Out'

所以你必须使用IN运算符并使用分组来计算每个状态的单个出现次数:

SELECT `State`, COUNT(*)
FROM dtr 
WHERE State IN ('Check In', 'Out', 'Out Back', 'Check Out')
GROUP BY `State`;

结果:

Check In  | 1
Out       | 3
Out Back  | 3
Check Out | 1