MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
treedon
V2EX  ›  MySQL

根据 a 表字段的值不同,统计 b 表字段的总和

  •  
  •   treedon · Sep 22, 2017 · 4074 views
    This topic created in 3155 days ago, the information mentioned may be changed or developed.
    a 订单详情表
    b 存储商品信息表
    c 存储商品价格表(表已经改不了,成型了 3 表已经关联)

    b 表大概结构 c 表大概结构
    id pay_type || id| price
    1 weixin || 1| 5
    2 zhifubao || 2|6
    3 zhifubao || 3|7
    4 weixin || 4|8

    分别显示统计
    a.pay_type =weixin 的总价钱
    a.pay_type =zhifubao 的总价钱

    试过 sum(b.pay_type=weixin) as weixin
    sum(b.pay_type=zhifubao) as zhifubao 好像不对
    7 replies    2017-09-25 13:09:03 +08:00
    Clarekang
        1
    Clarekang  
       Sep 22, 2017
    select sum(price) from c group by pay_type
    treedon
        2
    treedon  
    OP
       Sep 22, 2017
    @Clarekang 我已经用了 group by create_time 了
    还可以用 gruop by pay_type 么
    treedon
        3
    treedon  
    OP
       Sep 22, 2017
    t t 40 位查看
    只有一条回复,不知道什么情况 t t
    surfire91
        4
    surfire91  
       Sep 22, 2017
    描述问题不全面啊,有朋友指出用 group by, 你又说已经用了,问还能不能用,当然还能用,但是你知道怎么用了么?
    Mac
        5
    Mac  
       Sep 22, 2017
    group by create_time,pay_type
    x8
        6
    x8  
       Sep 23, 2017
    SELECT tbl_b.pay_type, sum(tbl_c.price)
    FROM tbl_b,tbl_c
    WHERE tbl_b.id=tbl_c.id
    GROUP BY tbl_b.pay_type
    treedon
        7
    treedon  
    OP
       Sep 25, 2017
    已经解决了
    sum(if(a.pay_type=1),b.price,0),
    sum(if(a.pay_type=0),b.price,0),
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3235 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 12:51 · PVG 20:51 · LAX 05:51 · JFK 08:51
    ♥ Do have faith in what you're doing.