• MySql中group_concat的使用


    CREATE TABLE `staff` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NULL DEFAULT NULL COMMENT '姓名',
    `salary` BIGINT(20) NULL DEFAULT NULL COMMENT '薪水',
    `depart` VARCHAR(50) NULL DEFAULT NULL COMMENT '部门',
    PRIMARY KEY (`id`)
    )
    COMMENT='职工表'
    ENGINE=InnoDB;
    
    INSERT INTO `staff` (`id`, `name`, `salary`, `depart`) VALUES (1, '小李', 5000, 'it部门');
    INSERT INTO `staff` (`id`, `name`, `salary`, `depart`) VALUES (3, '小红', 3000, '人事');
    INSERT INTO `staff` (`id`, `name`, `salary`, `depart`) VALUES (2, '张三', 2000, '财务');
    
    以下有几种查询sql:
    1、select a.depart, sum(a.salary) from staff a group by a.depart;


    2、select a.depart, concat("price=" , sum(a.salary)) from staff a group by a.depart;

    3、select a.depart, group_concat("depart=",a.depart, ",price=", sum(a.salary)) from staff a group by a.depart;

    4、以上可以更改为:

    select group_concat("depart=",c.depart, " price=",c.result) from staff a leftjoin (select b.depart, sum(b.salary) as result from staff b group by b.depart) as c on c.depart = a.depart;
    

    在网上看到一种说法:The SUM and AVG functions are not procedural. They are done in parallel. Which means that they can't be dependent.

    总结一下,a、对于group_concat中使用聚合函数的情况,可以选择先计算聚合函数,再引用。b、可以对查询结果取别名

     

  • 相关阅读:
    xilinx下载器,JTAG-HS3和Platform Cable USB II 速度对比
    LATTICE下载器HW-USBN-2B使用说明
    altera下载器高速版本 PL-USB2-BLASTER 使用说明
    FPGA流程设计
    关于fpga的后仿真重要性
    使用MyBatis分页插件PageHelper遇到的问题
    java 面试题总结01
    netty Demo
    netty 概念篇
    pom.xml 简述
  • 原文地址:https://www.cnblogs.com/mucheng/p/5912587.html
Copyright © 2020-2023  润新知