• MySQL函数GROUP_CONCAT() 实现多条数据合并


    group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来,group by指定的列进行分组。

    例如:

        -- 根据物流订单id查询所有物流订单,车源订单,车辆信息(多条数据合并为一条数据,英文逗号隔开)
            SELECT
                a.id AS logisOrderId,
                a.logistics_order_code AS logisOrderCode,
                a.logis_order_sum_fee AS logisOrderSumFee,
                GROUP_CONCAT(b.id) AS carSourceOrderId,
                b.start_city_name AS startCityName,
                b.end_city_name AS endCityName,
                b.start_city_id AS startCityId,
                b.end_city_id AS endCityId,
                GROUP_CONCAT(b.buyer_id) AS buyerId,
                GROUP_CONCAT(b.buyer_name) AS buyerName,
                GROUP_CONCAT(b.buyer_phone) AS buyerPhone,
                GROUP_CONCAT(c.id) AS carSourceOrderItemId,
                GROUP_CONCAT(d.car_brand) AS carBrand,
                GROUP_CONCAT(d.car_type) AS carType,
                GROUP_CONCAT(d.colour) AS carColor,
                sum(c.insurance_fee) AS insuranceSumFee,
                sum(c.actual_fee) AS actualSumFee,
                GROUP_CONCAT(d.car_id) AS carId
            FROM
                t_ims_logistics_order a
              LEFT JOIN t_ims_carsource_order b ON a.id=b.logistics_order_id
              LEFT JOIN t_ims_carsource_order_item c ON  b.id = c.car_source_order_id
              LEFT JOIN t_ims_car d ON c.logistics_car_id = d.id
            WHERE  
             a.id=11
            GROUP BY a.id

     合并前:两条数据

     合并后:一条数据

    链接:https://blog.csdn.net/mary19920410/article/details/76545053

  • 相关阅读:
    【z02】选择客栈
    JavaEE(22)
    驱动表问题
    影响子查询展开的情况
    SQL*Net message from client
    等值链接和非等值链接
    ||拼接列关联和直接关联区别
    分页SQL优化
    SQL*Net more data to client
    SQL*Net message to client
  • 原文地址:https://www.cnblogs.com/smileblogs/p/9207774.html
Copyright © 2020-2023  润新知