0

我的 MySQL 数据库中有两个表(tbl1tbl2)。现在,我编写了如下查询来生成 in_quantity 和 out_quantity 的总和,其中serialno = AAA1-

SELECT 
(
    SELECT 
        #GROUP_CONCAT(in_quantity), 
        SUM(in_quantity) GP FROM stockin WHERE serialno = 'AAA1'
) A,
(
    SELECT 
        #GROUP_CONCAT(out_quantity),
        SUM(out_quantity) SOP FROM stockout WHERE serialno = 'AAA1'
) B

此查询输出如下 -

+------+------+
| A    | B    |
+------+------+
|  800 |  740 |
+------+------+
1 row in set (0.00 sec)

现在,我需要以下结果-

+------+------+------+
| A    | B    | C    |
+------+------+------+
|  800 |  740 |   60 |
+------+------+------+
1 row in set (0.00 sec)

我怎样才能在我的查询中写这个?我在 stackoverflow.com 上检查了几个线程。但是,没有关于(AS A - AS B) = AS C

4

1 回答 1

0

使用您的“A”“B”查询作为子查询:

SELECT q.A, q.B, q.A-q.B as C FROM
  (SELECT 
  (
    SELECT 
        #GROUP_CONCAT(in_quantity), 
        SUM(in_quantity) GP FROM stockin WHERE serialno = 'AAA1'
  ) A,
  (
    SELECT 
        #GROUP_CONCAT(out_quantity),
        SUM(out_quantity) SOP FROM stockout WHERE serialno = 'AAA1'
  ) B
  --following code
) q
于 2013-06-24T09:23:48.300 回答