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 · Jul 13, 2016 · 2403 views
    This topic created in 3593 days ago, the information mentioned may be changed or developed.

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

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

    4 replies    2016-07-13 20:42:13 +08:00
    petelin
        1
    petelin  
       Jul 13, 2016 via Android
    可以考虑 join
    delavior
        2
    delavior  
       Jul 13, 2016
    必须再嵌套一层,定义的别名在同一个 select 里肯定是无效的
    petelin
        3
    petelin  
       Jul 13, 2016
    `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  
       Jul 13, 2016
    SELECT SUM(a.cost - b.cost)
    FROM a
    LEFT JOIN b ON (a.id = b.id)
    WHERE a.id = xxx;
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2525 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 00:34 · PVG 08:34 · LAX 17:34 · JFK 20:34
    ♥ Do have faith in what you're doing.