zoukankan      html  css  js  c++  java
  • 有人在群里问关于SQL表组合数据问题

    他的问题如下

    如此我建表如下:

    如果想根据用户进行分组后 又要显示所属门店在同一个字段中的话,这里需要用group_concat来显示 同时关联的时候可用find_in_set来处理

    我设计的SQL如下:

    select vname,group_concat(name)as name,total_sum from vip 
    left join (
        select vipid,group_concat(storeid) as store_id,sum(paidin) as total_sum from `order` group by vipid
    ) as t 
    on vip.id=t.vipid  
    left  join store 
    on find_in_set(store.id,t.store_id) group by vname;

    效果如图:

    虽然说有点令人不可思议,不过SQL就是这么神奇,令人称道啊,哈哈哈!

    后来 他又整一列出来,净出幺蛾子,要求这样

    我想到刚刚的方式不能那样做了,看来要改一个方式得到

    select t.vname,t.name,GROUP_CONCAT(store.`name`),t.total_sum from store left JOIN
    (
        select storeid,vname,name,store_id,total_sum from vip 
        left join store on vip.storeid=store.id
        left join 
        (
            select vipid,group_concat(storeid) as store_id,sum(paidin) as total_sum from `order` group by vipid
        ) as tmp on .tmp.vipid=vip.id
    ) as t
    on find_in_set(store.id,t.store_id)
    group by t.storeid,t.vname;

    效果如图:

  • 相关阅读:
    EV: There is no source code available for the current location.
    EV: 致新教育萤火虫父母们
    DEL: 2012年每月花销
    DEL: 博客分类规则
    java编程规范
    maven随笔
    JMS组成结构与特点
    activemq 的简单使用
    activeMQ安装与访问
    java8 stream
  • 原文地址:https://www.cnblogs.com/lizhaoyao/p/8521969.html
Copyright © 2011-2022 走看看