我想按日期选择我的数据 - 从日期到另一个日期,所以我有这个查询,
SELECT * FROM mytalbe WHERE date BETWEEN '2014-10-09' AND '2014-10-10'
但此查询仅返回2014-10-09'中的数据,不包括“2014-10-10”中的数据,除非我将查询更改为以下内容,
SELECT * FROM mytalbe WHERE date BETWEEN '2014-10-09' AND '2014-10-11'
这不是一个理想的解决方案。如何选择包括' 2014-10-10'?
中的数据的数据注意:
我认为我的问题与其他重复问题不同,
我的数据样本......
sid nid timestamp date
1 20748 5 1412881193 2014-10-09 14:59:53
2 20749 5 1412881300 2014-10-09 15:01:40
3 20750 5 1412881360 2014-10-09 15:02:40
答案 0 :(得分:12)
如果日期是时间戳,您需要执行以下操作:
SELECT * FROM mytalbe WHERE date BETWEEN '2014-10-09 00:00:00' AND '2014-10-10 23:59:59'
或者你可以做,我相信:
SELECT * FROM mytalbe WHERE DATE(date) BETWEEN '2014-10-09' AND '2014-10-10'
或者,因为它是一个文本字段:
SELECT * FROM mytalbe WHERE DATE_FORMAT(date,'%Y-%m-%d') BETWEEN '2014-10-09' AND '2014-10-10'
答案 1 :(得分:4)
您也可以不使用between
。
select * from mytable where `date` >= '2014-10-09' and `date` <= '2014-10-10'
示例:
mysql> create table dd (id integer primary key auto_increment, date text);
Query OK, 0 rows affected (0.11 sec)
mysql> insert into dd(date) values ('2014-10-08'), ('2014-10-09'), ('2014-10-10'), ('2014-10-11');
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from dd where date >= "2014-10-09" and date <= "2014-10-10";
+----+------------+
| id | date |
+----+------------+
| 2 | 2014-10-09 |
| 3 | 2014-10-10 |
+----+------------+
2 rows in set (0.01 sec)
因为它包含时间,你不想要时间。这样:
select substring(date, 1, 10) from dd where substring(date, 1, 10) between '2014-10-09' and '2014-10-10';
问题再次更新,其他答案
唉。你有时间戳字段?在这种情况下:
select date(from_unixtime(timestamp)) from mytabel where date(from_unixtime(timestamp)) between '2014-10-09' and '2014-10-10'
最后我们到达了sqlite
select date(datetime(timestamp, 'unixepoch'))
from mytable
where date(datetime(timestamp, 'unixepoch'))
between '2014-10-09' and '2014-10-10';
答案 2 :(得分:1)
同样的问题解决了,谢谢。 我的代码:
var FechaInicio = dtpFechaInicial.Value.ToString("yyyy-MM-dd");
var FechaFinal = dtpFechaFinal.Value.ToString("yyyy-MM-dd");
string SQLcmd = $"SELECT * FROM sorteos WHERE DATE(fecha) BETWEEN ('{FechaInicio}') AND ('{FechaFinal}')";