[Original] use the GROUP_CONCAT function of MySQL to implement aggregate Multiplication

Source: Internet
Author: User
The MySQL aggregate function provides addition, average, minimum, and maximum, but not multiplication. Here we use the existing MYSQL GROUP_CONCAT function to implement aggregation multiplication. Create an example Table: CREATETABLE 'tb _ seq '('num' int (10) NOTNULL, 'seq _ type' enum (yellow, green, red) NOTNULL) ENGINEInn

The MySQL aggregate function provides addition, average, minimum, and maximum, but not multiplication. Here we use the existing MYSQL GROUP_CONCAT function to implement aggregation multiplication. Create an example Table: CREATETABLE 'tb _ seq '('num' int (10) NOTNULL, 'seq _ type' enum ('yellow', 'green ', 'red') NOTNULL) ENGINE = Inn

The MySQL aggregate function provides addition, average, minimum, and maximum, but not multiplication. Here we use the existing MYSQL GROUP_CONCAT function to implement aggregation multiplication.

Create an example table first:

CREATE TABLE `tb_seq` (  `num` int(10) NOT NULL,  `seq_type` enum('yellow','green','red') NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Insert sample data:

insert  into `tb_seq`(`num`,`seq_type`) values (4,'green'),(1,'red'),(3,'green'),    (1,'red'),(8,'red'),(4,'yellow'),    (8,'red'),(7,'yellow'),(10,'red'),    (1,'red'),(1,'red'),(1,'yellow'),    (5,'green'),(9,'red'),(1,'yellow'),    (6,'yellow');


Create a string multiplication method based on the comma separator, provided that the strings are separated by commas.

DELIMITER $$USE `t_girl`$$DROP FUNCTION IF EXISTS `func_multiple`$$CREATE DEFINER=`root`@`localhost` FUNCTION `func_multiple`(    f_nums VARCHAR(1000)    ) RETURNS DOUBLE(10,2)BEGIN      -- Created by ytt 2014/10/21.      DECLARE result DOUBLE(10,2) DEFAULT 1;      DECLARE cnt,i INT DEFAULT 0;            SET cnt = CHAR_LENGTH(f_nums) - CHAR_LENGTH(REPLACE(f_nums,',','')) + 1;            WHILE i < cnt      DO        -- get multiple result.        SET result = result * REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(f_nums,',',i+1)),',',1));        SET i = i + 1;      END WHILE;      SET result = ROUND(result,2);      RETURN result;       END$$DELIMITER ;


Now, we can use the functions I created and the GROUP_CONCAT aggregate function of MYSQL to conveniently implement multiplication.

SELECT seq_type,func_multiple(GROUP_CONCAT(num ORDER BY num ASC SEPARATOR ',')) AS multiple_num FROM tb_seq WHERE 1 GROUP BY seq_type;+----------+--------------+| seq_type | multiple_num |+----------+--------------+| yellow   |       168.00 || green    |        60.00 || red      |      5760.00 |+----------+--------------+3 rows in set (0.00 sec)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.