› 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
fatebe
V2EX  ›  MySQL

计算用户消费总额, MySQL 查询问题请教?

  •  
  •   fatebe · Oct 11, 2016 · 6436 views
    This topic created in 3675 days ago, the information mentioned may be changed or developed.

    目前有两表,一个用户信息表(users),一个消费记录表(shopping)。

    想要查询有消费记录的用户信息,并计算出消费总额。

    我尝试如下 select u.id,u.name,f.money from data.users u ,data.shopping s where s.users_id = u.id

    但这样查询出来的每条单独的消费记录和用户信息,我想将消费总金额合并 SUM(money),最终显示出每个用户的消费总金额,请问该如何写?

    5 replies  •  2016-10-11 16:32:27 +08:00
    Hyeongo
    Hyeongo
    Oct 11, 2016 via iPhone
    1
    group by
    SeanChense
    SeanChense
    Oct 11, 2016
    2
    `select u.id,u.name, SUM(f.money) from data.users u ,data.shopping s where s.users_id = u.id group by u.id`

    酱紫?
    alouha
    alouha
    Oct 11, 2016
    3
    select shop.*,u.name,u.uid (select SUM(money) as s,id from shopping group by uid ) as shop,users where shop.id=u.users_id
    fatebe
    fatebe
    OP
    Oct 11, 2016
    4
    @Hyeongo
    @SeanChense
    厉害了我的哥,是要这个效果
    刚开始学习,感谢。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1407 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:59 · PVG 05:59 · LAX 13:59 · JFK 16:59
    ♥ Do have faith in what you're doing.