没有表格显示日期范围

时间:2019-02-22 19:04:01

标签: jquery mysql sql mariadb

有没有可能在mysql表中显示日期范围? 我有此代码,但缺少。

SET @startDate = '2018-10-01';
SET @endDate = '2019-02-21';

select @startDate + INTERVAL seq.seq DAY AS sequential_day
  from (
    SELECT A.N + 5*(B.N + 5*(C.N + 5*(D.N + 5*(E.N + 5*(F.N))))) AS seq
      FROM (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS A
      JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS B
      JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS C
      JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS D
      JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS E
      JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS F
       ) AS seq
 where seq.seq <= @endDate

它显示从2018-10-012024-04-11吗? 我的查询有问题,有什么想法吗?我想显示从2018-10-012019-02-21

我正在使用mariaDB

3 个答案:

答案 0 :(得分:1)

我在这里得到了答案:

How to get list of dates between two dates in mysql select query

SET @startDate = '2018-10-01';
SET @endDate = '2019-02-21';

select * from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between @startDate and @endDate

答案 1 :(得分:1)

您可以使用 SEQUENCE 引擎,例如:

select CURDATE() + INTERVAL seq day  FROM seq_1_to_31;

这是完整的示例:

SELECT '2018-10-01' + interval seq day as my_date FROM seq_0_to_9999
WHERE '2018-10-01' + interval seq day  <= '2019-02-21';

您可以在这里找到详细信息:https://mariadb.com/kb/en/library/sequence-storage-engine/

查看安装了哪些引擎

MariaDB [test]> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                                          | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| MRG_MyISAM         | YES     | Collection of identical MyISAM tables                                            | NO           | NO   | NO         |
| CSV                | YES     | Stores tables as CSV files                                                       | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables                        | NO           | NO   | NO         |
| MyISAM             | YES     | Non-transactional engine with good performance and small data footprint          | NO           | NO   | NO         |
| Aria               | YES     | Crash-safe tables with MyISAM heritage                                           | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, foreign keys and encryption for tables | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                                               | NO           | NO   | NO         |
| SEQUENCE           | YES     | Generated tables filled with sequential values                                   | YES          | NO   | YES        |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
8 rows in set (0.006 sec)

MariaDB [test]> 

样品

MariaDB [test]> select seq FROM seq_1_to_4;
+-----+
| seq |
+-----+
|   1 |
|   2 |
|   3 |
|   4 |
+-----+
4 rows in set (0.042 sec)

MariaDB [test]> select CURDATE() + INTERVAL seq day  FROM seq_1_to_31;
+--------------------------------+
| DATE(NOW()) + INTERVAL seq day |
+--------------------------------+
| 2019-02-23                     |
| 2019-02-24                     |
| 2019-02-25                     |
| 2019-02-26                     |
| 2019-02-27                     |
| 2019-02-28                     |
| 2019-03-01                     |
| 2019-03-02                     |
| 2019-03-03                     |
| 2019-03-04                     |
| 2019-03-05                     |
| 2019-03-06                     |
| 2019-03-07                     |
| 2019-03-08                     |
| 2019-03-09                     |
| 2019-03-10                     |
| 2019-03-11                     |
| 2019-03-12                     |
...
...
| 2019-03-22                     |
| 2019-03-23                     |
| 2019-03-24                     |
| 2019-03-25                     |
+--------------------------------+
31 rows in set (0.019 sec)

MariaDB [test]> 

答案 2 :(得分:0)

使用BETWEEN作为值的范围。作为查询本身,您希望日期在某个范围内

您当前的查询显示所有<= some_value并没有落在您希望日期位于的特定范围内。 BETWEEN允许您提供一系列值。