利用MySQL 的GROUP_CONCAT函數實現彙總乘法,mysqlgroup_concat

來源:互聯網
上載者:User

利用MySQL 的GROUP_CONCAT函數實現彙總乘法,mysqlgroup_concat
MySQL 彙總函式裡面提供了加,平均數,最小,最大等,但是沒有提供乘法,我們這裡來利用MYSQL現有的GROUP_CONCAT函數實現彙總乘法。
先建立一張樣本表:

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



插入樣本資料:
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');




建立基於逗號分隔字元的字串乘法,前提是字串逗號分隔的都是數字。
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 ;




好了,我們利用我建立的函數以及MYSQL內建的GROUP_CONCAT彙總函式就可以很方便的實現乘法了。



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)





Oracle有沒有類似MySQL的group_concat()功可以的函數

wm_concat
需要安裝一個包,在rdbms\admin\下
 
mysql裡有個group_concat函數,達夢資料庫裡有沒有類似的函數?

這個函數是mysql專有的函數,其他資料庫沒有類似函數
 

相關文章

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.