V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
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
Mac
V2EX  ›  MySQL

两个子查询间运算有没有简便方法?

  •  
  •   Mac · 2016-07-13 18:17:52 +08:00 · 1946 次点击
    这是一个创建于 2845 天前的主题,其中的信息可能已经有所发展或是发生改变。

    select (select sum(cost) from a where id=xxx) ys, (select sum(cost) from b where id=xxx) yf, ys-yf from abc

    我这么写貌似是错的,必须再写一遍两个子查询用减号相连么?

    4 条回复    2016-07-13 20:42:13 +08:00
    petelin
        1
    petelin  
       2016-07-13 19:46:03 +08:00 via Android
    可以考虑 join
    delavior
        2
    delavior  
       2016-07-13 19:51:29 +08:00
    必须再嵌套一层,定义的别名在同一个 select 里肯定是无效的
    petelin
        3
    petelin  
       2016-07-13 19:53:59 +08:00
    `select a.sum - a.sum from (select id,sum(in_money) as sum from A where id = 0) as a,(select id,sum(out_money) as sum from B where id = 0) as b;`

    `select (select sum(in_money) from A where id = 0) - (select sum(out_money) from B where id = 0);`
    klesh
        4
    klesh  
       2016-07-13 20:42:13 +08:00
    SELECT SUM(a.cost - b.cost)
    FROM a
    LEFT JOIN b ON (a.id = b.id)
    WHERE a.id = xxx;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3684 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:22 · PVG 12:22 · LAX 21:22 · JFK 00:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.