简体   繁体   English

从数据库中的日期检查日期输入的范围

[英]Checking the range of the date input from the date on the database

I have a table named nca and heres what it looks like with values : 我有一个名为nca的表,它具有值的外观:

+--------+---------+------------+------------+--------------+---------+
| nca_id | nca_no  | issue_date | nca_amount | account_type | balance |
+--------+---------+------------+------------+--------------+---------+
|      1 | 14-0001 | 2015-01-08 |     200000 | ROP          |       0 |
|      2 | 14-0002 | 2015-01-08 |     400000 | ROP          |       0 |
|      3 | 14-0003 | 2015-02-02 |    1000000 | ROP          |       0 |
|      4 | 14-0004 | 2015-02-02 |     300000 | ROP          |       0 |
|      5 | 14-0001 | 2015-01-08 |     250000 | DBP-TRUST    |       0 |
|      6 | 14-0002 | 2015-01-08 |     400000 | DBP-TRUST    |       0 |
+--------+---------+------------+------------+--------------+---------+

Now using this query it can display the SUM of the nca_amount base on the issue_date: 现在,使用此查询,它可以基于issue_date显示nca_amount的总和:

SELECT SUM(nca_amount) FROM nca WHERE account_type = 'ROP' AND issue_date = '2015-02-02'

which gives a result of 1300000 since it adds all the amount that having a date of 2015-02-02 and just works fine if you set the date just on the query itself. 给出的结果为1300000,因为它将所有具有日期2015-02-02的金额相加,如果仅在查询本身上设置日期,则效果很好。

But I have scenario that I want to solve. 但是我有要解决的情况。 I have an html form where the user could enter the date he want. 我有一个HTML表单,用户可以在其中输入他想要的日期。

And if the user would type the date of 2015-02-23, I want this date input referring to the issue_date of 2015-02-02 from my table. 并且如果用户输入日期2015-02-23,我希望此日期输入引用我表中的2015-02-02的issue_date。 What I want is if the date input is in the RANGE of the MONTH of the issue_date on the nca table, this would refer to the issue_date found on the nca table and display the sum. 我想要的是,如果输入的日期在nca表上的issue_date的MONTH的范围内,这将引用在nca表上找到的issue_date并显示总和。 For example : 例如 :

user input_date = '2015-02-23' referring to issue_date = '2015-02-02' 用户input_date ='2015-02-23'指的是issue_date ='2015-02-02'

as the inputted date is in the range of '2015-02-02' found on my nca table and then it can display the sum like on my query above. 因为输入的日期在我的nca表中找到的'2015-02-02'范围内,所以它可以像上面的查询一样显示总和。 How can I do this query? 我该如何查询? Can any one help with this one? 有人可以帮忙吗? thanks in advance. 提前致谢。

If you want to match the month: 如果要匹配月份:

SELECT SUM(nca_amount)
FROM nca
WHERE account_type = 'ROP' AND
      year(issue_date) = year('2015-02-02') and
      month(issue_date) = month('2015-02-02');

However, this doesn't allow the query to use an index on issue_date . 但是,这不允许查询在issue_date上使用索引。 For that, you need to rephrase the query as: 为此,您需要将查询改写为:

SELECT SUM(nca_amount)
FROM nca
WHERE account_type = 'ROP' AND
      issue_date >= date(concat(left('2015-02-02', 7), '-01')) and
      issue_date <  date(concat(left('2015-02-02', 7), '-01')) + interval 1 month;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM