mysql user variable assignement with count(*)

落爺英雄遲暮 提交于 2021-02-17 06:54:19

问题


I try to do query with a user variable, but I don't understand why, i can't update the variable in the query. I want to sum the grouped result for each day to do a chart. That really simple.

SET @total := 0;
SELECT 
    "total Register",
    li.registerDate,
    @total := COUNT(*) + @total as registerNumber,
    @total 
    FROM calendar c3
    INNER JOIN (
            SELECT 
            c2.ide_calendar as ide_calendar,
            c2.name,
            DATE_FORMAT(i.date_creation, "%Y-%m-%d") as registerDate
            FROM calendrar c2
            LEFT JOIN inscription i ON i.ide_calendrar = c2.id
    ) li ON li.ide_calendrar = c3.id
    WHERE c3.id = 9291
    GROUP BY registerDate

That really strange. I looking mysql documentation, and nothing works.

EDIT : I want for some statistics to have for each day the total number of register.

Day 1 : 2 register then total = 2
Day 2 : 1 register then total = 3.

I used it by the past, and had no problem.

EDIT 2 : Exemple of the actual data result about the query.

total Inscription   |   2017-07-10  |   2
total Inscription   |   2020-04-20  |   2
total Inscription   |   2020-04-21  |   3
total Inscription   |   2020-06-17  |   4
total Inscription   |   2020-06-18  |   2

What I want is :

total Inscription   |   2017-07-10  |   2
total Inscription   |   2020-04-20  |   4
total Inscription   |   2020-04-21  |   7
total Inscription   |   2020-06-17  |   11
total Inscription   |   2020-06-18  |   13

That's why I used variables. It's more simple to use on query. But this time, i don't understand.

Thanks a lot.


回答1:


As @JagaSrik said, there is a need to more information about your data and tables to give right answer. However, if you need to change a table like this:

name                |     date      |  id
-----------------------------------------
total Inscription   |   2017-07-10  |   2
total Inscription   |   2020-04-20  |   2
total Inscription   |   2020-04-21  |   3
total Inscription   |   2020-06-17  |   4
total Inscription   |   2020-06-18  |   2

to something like this:

     date      |  total
-----------------------------------------
   2017-07-10  |   2
   2020-04-20  |   4
   2020-04-21  |   7
   2020-06-17  |   11
   2020-06-18  |   13

you can use CURSOR.

For this example:

DELIMITER $$
CREATE PROCEDURE `countIds`()
BEGIN
    DECLARE finished INTEGER DEFAULT 0;
    DECLARE total INTEGER DEFAULT 0;
    DECLARE theCount INTEGER DEFAULT 0;
    DECLARE theDate varchar(100) DEFAULT "";    

        DEClARE curCount 
        CURSOR FOR 
            SELECT `id`,`date` FROM table1;

        DECLARE CONTINUE HANDLER 
    FOR NOT FOUND SET finished = 1;
        
    OPEN curCount;

    getCount: LOOP
        FETCH curCount INTO theCcount,theDate;
        IF finished = 1 THEN 
            LEAVE getCount;
        END IF;
        SET total = total + theCount;
        select theDate, total;
    END LOOP getCount;
    CLOSE curCount;       

END$$
DELIMITER ;

Nevertheless CURSOR is a way to do your work and there may be more effective methods.



来源:https://stackoverflow.com/questions/63134369/mysql-user-variable-assignement-with-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!