没有时间使用DATETIME的MYSQL SELECT语句

时间:2016-07-05 16:58:53

标签: php mysql sql-server datetime

这非常有效:

SELECT DISTINCT customers 
FROM customertable 
WHERE time_paid BETWEEN '2016-06-28 10:27:55' AND '2016-07-05 14:07:51';

但是,由于我使用javascript datepicker为用户选择日期,并且只有日期但没有时间这显然不起作用:

SELECT DISTINCT customers 
FROM customertable 
WHERE time_paid BETWEEN '2016-06-28' AND '2016-07-05';

我的选择声明是这样的:$t1是较低的日期而$t2是较高的日期。

$sql = "SELECT DISTINCT customertable FROM customer WHERE time_paid BETWEEN '$t1' AND '$t2'";

$result = mysqli_query($conn, $sql);
$rowcount = mysqli_num_rows($result);

有没有办法可以在我的select语句中插入连接日期,比如'$t1'.12.00.00,以便在选择时处理数据库中的完整DATETIME条目?

1 个答案:

答案 0 :(得分:1)

使用mysql函数date add

mysql> SELECT '2008-01-02';
+------------+
| 2008-01-02 |
+------------+
| 2008-01-02 |
+------------+
1 row in set (0.00 sec)

mysql> SELECT ADDDATE('2008-01-02', INTERVAL 0 HOUR);
+----------------------------------------+
| ADDDATE('2008-01-02', INTERVAL 0 HOUR) |
+----------------------------------------+
| 2008-01-02 00:00:00                    |
+----------------------------------------+
1 row in set (0.00 sec)


SELECT DISTINCT customertable FROM customer WHERE time_paid BETWEEN ADDDATE('$t1',INTERVAL 0 HOUR) AND ADDDATE('$t2',INTERVAL '23:59' HOUR_MINUTE)";