简体   繁体   English

PHP计算每天节省的文档总数

[英]PHP Calculate total document saves per day

I'm trying to get the total document saves per day. 我正在尝试每天节省的文件总数。 But i don't have any idea how. 但是我不知道如何。 Here are the Columns along with their data in my Database. 这是我的数据库中的列及其数据。

report_id    doc_date   total_doc 
- 1          2017-06-06  1  
- 2          2017-06-23  1  
- 3          2017-06-14  1  
- 4          2017-06-12  1  
- 5          2017-06-16  1
- 6          2017-06-21  1
- 7          2017-06-14  1
- 9          2017-06-13  1
- 10         2017-06-21  1
- 11         2017-06-12  1
- 12         2017-06-18  1
- 13         2017-06-12  1
.....

Above, there are similar date, for example, 上面有类似的日期,例如,

- 3          2017-06-14  1
- 7          2017-06-14  1 

I need a sql query which would calculate and show the total document saves per day. 我需要一个SQL查询,该查询将计算并显示每天节省的总文档数。 So for 14 of June 2017, the total would be 2. Any idea guys? 因此,对于2017年6月14日,总数为2。 FYI the total_doc is automatically equal to 1. Updated: And show the total document saves in that day. 仅供参考,total_doc自动等于1。 更新:并显示当天保存的文档总数。

Try this (set table_name to the name of your table): 尝试以下操作(将table_name设置为表的名称):

SET @row_count = 0;
SELECT (@row_count:=@row_count+1) AS totalSaves FROM table_name WHERE doc_date='2017-06-14'

The first line defines a variable, row_count , and sets it to 0 . 第一行定义变量row_count ,并将其设置为0

The second line goes through the database, finds all rows where the date is 2017-06-14 , and for each one increments row_count . 第二行遍历数据库,查找日期为2017-06-14所有行,每增加一行row_count

Then, we SELECT the final number stored in row_count as a single row in a column labeled as totalSaves . 然后,我们在标记为totalSaves的列中SELECT存储在row_count作为单行的最终数字。

Set table_name to the name of your table table_name设置为表的名称

SELECT doc_date, sum(total_doc) AS total 
FROM table_name
GROUP BY doc_date

Change table_name with your table name. 用表名更改table_name。

If you just want show 1 date : 如果您只想显示1个日期:

SELECT doc_date, sum(total_doc) AS total 
FROM table_name
WHERE doc_date = '2017-06-14'
GROUP BY doc_date
SELECT 
    doc_date,
    count(doc_date) AS saves
FROM documents
GROUP BY doc_date
ORDER BY doc_date ASC;

OR 要么

SELECT
    doc_date,
    sum(total_doc) AS saves
FROM documents
GROUP BY doc_date
ORDER BY doc_date ASC;

Notices: 注意事项:

1) If you are using the first option, then you can remove the column total_doc . 1)如果使用第一个选项,则可以删除列total_doc That would also be my advice. 这也是我的建议。 You have a good mysql format of the date values in the doc_date column, so you can make solid operations on it. doc_date列中的日期值具有良好的mysql格式,因此您可以对其进行可靠的操作。

2) You can sort the records in a descending order by doc_date , if you wish. 2)如果需要,可以按doc_date降序对记录进行排序。

3) Don't forget to alias (like I did with AS saves ) the columns when using functions like count or sum . 3)当使用诸如countsum类的函数时,不要忘记对列进行别名(就像我对AS saves所做的那样)。 Because then you can use a HAVING filter also. 因为这样您也可以使用HAVING过滤器。 So something like this - all documents with more than one save per day: 像这样-每天保存一次以上的所有文档:

SELECT 
    doc_date,
    count(doc_date) AS saves
FROM documents
GROUP BY doc_date
HAVING saves > 1
ORDER BY doc_date ASC;

And then you can also filter by only one saved document on a specified day: 然后,您还可以在指定的日期仅按一个保存的文档进行过滤:

SELECT 
    doc_date,
    count(doc_date) AS saves
FROM documents
WHERE doc_date = "2017-06-12"
GROUP BY doc_date
HAVING saves > 1
ORDER BY doc_date ASC;

4) Or you can also just ignore HAVING and doc_date if you wish. 4)或者,您也可以根据需要忽略HAVINGdoc_date That would give you the documents saved on a specified day. 这将为您提供在指定日期保存的文档。

SELECT count(doc_date) AS saves
FROM documents
WHERE doc_date = "2017-06-12"
GROUP BY doc_date
ORDER BY doc_date ASC;

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

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