简体   繁体   English

计算总数

[英]calculate total count

We have table structure and data like我们有表结构和数据,如

Id | startdate | enddate   | price
1. |  1 jan    | 30 Jan    | 100
2  |  1 Feb    | 28 Feb    | 200
3. |  1 March  |  31 March | 300

Now I want to calculate total price between the date range.现在我想计算日期范围之间的总价。 Like from 1 Jan to 28 Feb total count will be 300, from 1 Feb to 31 March total count will.be 500, how to write query for this比如从 1 月 1 日到 2 月 28 日的总数将是 300,从 2 月 1 日到 3 月 31 日的总数将是 500,如何为此编写查询

Something like this,像这样的东西,

SELECT SUM(price) FROM [TABLE_NAME] WHERE DATE BETWEEN '2012-01-01' AND '2012-02-28'

Please note that this might not be the correct MySQL syntax, but you get the idea.请注意,这可能不是正确的 MySQL 语法,但您明白了。

如果这些存储为日期字段,您可以执行以下操作

Select SUM(Price) from Table where startdate > DD/MM/YYYY and enddate < DD/MM/YYYY

You need to store your dates in the DATE format.您需要以DATE格式存储日期。 Then we will simply perform a SUM on the table with the appropriate WHERE conditions.然后我们将简单地使用适当的WHERE条件对表执行SUM

SELECT SUM(price) FROM tablename WHERE startdate > '2012-01-01' AND enddate < '2012-02-28'

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

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