我有一个表名交易
+-------+---------------------+--------+
| t_id | date | amount |
+-------+---------------------+--------+
| 10 | 2016-04-17 19:24:05 | 1800 |
| 12 | 2016-06-11 12:40:13 | 200 |
| 17 | 2016-04-13 14:42:04 | 100 |
| 19 | 2016-05-14 17:45:43 | 1 |
| 20 | 2016-08-15 19:45:54 | 999 |
| 21 | 2016-01-17 11:46:02 | 1500 |
| 41 | 2016-02-18 17:23:14 | 500 |
| 42 | 2016-07-19 13:26:14 | 500 |
| 43 | 2016-02-18 17:23:15 | 500 |
| 44 | 2016-02-18 17:23:16 | 500 |
| 45 | 2016-02-18 18:23:16 | 500 |
| 46 | 2016-02-18 17:23:16 | 500 |
| 47 | 2015-10-18 14:23:17 | 500 |
| 48 | 2015-11-18 17:23:17 | 500 |
| 49 | 2015-12-18 11:23:18 | 500 |
| 50 | 2015-05-18 11:25:54 | 1000 |
| 51 | 2015-09-18 12:26:22 | 3000 |
| 52 | 2015-05-18 13:48:59 | 10 |
| 53 | 2015-03-18 15:48:59 | 10 |
| 54 | 2015-01-18 17:49:13 | 5000 |
+-------+---------------------+--------+
我想选择带日期的记录
我用
SELECT * FROM transactions WHERE date='2016-02-18';
我也在2个日期和我之间选择
SELECT * FROM transactions WHERE date<'2016-02-18' AND date>'2016-02-01';
但它不起作用。(我使用php Mysql xampp) 你能帮我理解这些概念吗?
答案 0 :(得分:2)
对于ist查询,您需要使用DATE()
函数,因为您的列类型为DATETIME
或TIMESTAMP
,因此您可以将其处理为:
SELECT * FROM transactions WHERE DATE(date) = '2016-02-18';
对于第二个查询,您只需将TIME添加为:
SELECT * FROM transactions WHERE date > '2016-02-01 00:00:00' AND date < '2016-02-18 23:59:59';
答案 1 :(得分:1)
是mysql DB吗?
如果不是使用DATE
功能:
SELECT * FROM transactions WHERE DATE(date)='2016-02-18';
OR
SELECT * FROM transactions WHERE date BETWEEN '2016-02-18' AND '2016-02-01';
答案 2 :(得分:1)
使用此
在查询中使用%和like。您将在给定日期获得所有结果
SELECT * FROM transactions WHERE date like '2016-02-18%';
答案 3 :(得分:0)
你应该使用BETWEEN ... AND ...
SELECT * FROM transactions WHERE `date` BETWEEN '2016-02-01' AND '2016-02-18';