Q
Q
Qairat2017-02-06 09:09:13
MySQL
Qairat, 2017-02-06 09:09:13

how to use while in mysql?

Hello!
It is necessary to add results through while, but something does not work. Here is the code:

BEGIN


  DECLARE population int;
    DECLARE total2 int DEFAULT 0 ;
    DECLARE irows int ;
 SET irows = (SELECT COUNT(d.Population) FROM MDepartments d LEFT JOIN Localities l ON d.LocalityId = l.Id WHERE d.ParentId = Parent AND Type = 0);

    IF irows>=1 THEN
      
      SELECT d.Population FROM MDepartments d LEFT JOIN Localities l ON d.LocalityId = l.Id WHERE d.ParentId = Parent AND Type = 0 INTO population;

    WHILE irows >= 0 DO
       	SET total = total+population;
       	END WHILE;
        
    ELSE
      
      SET total = 250;
    
   END IF;

  END

Here, several values ​​\u200b\u200bare returned, you need to add them to each other
SELECT d.Population FROM MDepartments d LEFT JOIN Localities l ON d.LocalityId = l.Id WHERE d.ParentId = Parent AND Type = 0 INTO population;

Help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2017-02-06
@Qairat

And why is such a horror needed?

SELECT IFNULL(SUM(`d`.`Population`), 250)
  FROM `MDepartments` AS `d`
  LEFT JOIN `Localities` AS `l` ON `l`.`Id` = `d`.`LocalityId`
  WHERE `d`.`ParentId` = :Parent AND `Type` = 0;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question