SQLite数据库-选择两个日期之间的数据?

wb1gzix0  于 2023-02-13  发布在  SQLite
关注(0)|答案(4)|浏览(150)

我想按日期选择我的数据-从一个日期到另一个日期,所以我有这个查询,

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'中数据的数据?

    • 注:**

我想我的问题和其他的重复问题不一样,
1.我的日期类型是TEXT
1.我需要选择不带时间的日期数据。
1.它是一个SQLite数据库。
我的示例数据:

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
yk9xbfzb

yk9xbfzb1#

如果date是时间戳,则需要执行以下操作:

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'
yhuiod9q

yhuiod9q2#

您也可以不使用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';
yx2lnoni

yx2lnoni3#

同样的问题解决了,谢谢.我的代码:

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}')";
ia2d9nvy

ia2d9nvy4#

我测试了这个代码片段:

SELECT local_date FROM (SELECT '2014-10-09 14:59:53' local_date UNION SELECT '2014-10-09 15:01:40' UNION SELECT '2014-10-09 15:02:40') WHERE local_date BETWEEN date('2014-10-09') AND date('2014-10-10')

它基本上返回:

2014-10-09 14:59:53
2014-10-09 15:01:40
2014-10-09 15:02:40

因此,您要查找的查询可能如下所示:

SELECT * FROM mytalbe WHERE date BETWEEN date('2014-10-09') AND date('2014-10-10')

我强烈推荐此官方文档:https://www.sqlite.org/lang_datefunc.html

相关问题