Using MySQL's group_concat function to realize aggregation multiplication

Source: Internet
Author: User

The MySQL aggregate function provides a plus, an average, a minimum, a maximum, etc., but does not provide multiplication, and here we use MySQL's existing group_concat function to achieve aggregate multiplication.
Create a sample table first:
CREATE TABLE ' tb_seq ' (  ' num ' int () not null,  ' Seq_type ' enum (' Yellow ', ' green ', ' red ') is not null) Engine=innodb D Efault 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 '), (Ten, ' Red '), (1, ' Red '), (1, ' Red '), (1, ' Yellow '), (5, '    Green '), (9, '    red '), (1, ' yellow '),    (6, ' yellow ');




Creates a string multiplication based on a comma delimiter, provided that the string is separated by a comma by a number.
DELIMITER $ $USE ' t_girl ' $ $DROP function IF EXISTS ' func_multiple ' $ $CREATE definer= ' root ' @ ' localhost ' FUNCTION ' func_ Multiple ' (    f_nums VARCHAR)    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;




Well, we use the functions I've created and the Group_concat aggregation functions from MySQL to make it easy to multiply.



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)




Using MySQL's group_concat function to realize aggregation multiplication

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.