如何从2个不同的表中获得结果?

时间:2014-09-20 13:04:45

标签: mysql sql

场景:我有2个查询表单2表,只想将两个查询结果作为单个查询结果查看。

详细信息:

表:loantrans

+-----+----------+---------+---------+---------+
| tid |   date   | account | purpose |    out  |
+-----+----------+---------+---------+---------+
|  1  |2014-08-12|   975   |  Loan   |   5000  |
|  2  |2014-08-12|   975   |Interest |   850   |
|  3  |2014-08-12|   975   |  Loan   |   150   |
|  4  |2014-08-12|   975   |Interest |   5000  |
+-----+----------+---------+---------+---------+

表:fdrtrans

+-----+----------+---------+---------+---------+
| tid |   date   | account | purpose |    out  |
+-----+----------+---------+---------+---------+
|  1  |2014-08-12|   975   |   FDR   |   5000  |
|  2  |2014-08-12|   975   |Interest |   850   |
|  3  |2014-08-12|   975   |   FDR   |   150   |
|  4  |2014-08-12|   975   | Deposit |   5000  |
+-----+----------+---------+---------+---------+

查询1:

SELECT MONTH(`loantrans`.`date`) as month, SUM(`loantrans`.`out`) AS loanout
            FROM loantrans
            WHERE  (`loantrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
            AND (`loantrans`.`purpose` = 'Loan')
            GROUP BY MONTH(`loantrans`.`date`)
            ORDER BY `loantrans`.`date`

结果:

+-------+---------+
| month | loanout |
+-------+---------+
|   1   |  28000  |
|   2   |  27000  |
|   3   |  10200  |
|   4   |  7000   |
|   5   |  95000  |
|   6   |  2000   |
+-------+---------+

查询2:

SELECT MONTH(`fdrtrans`.`date`) as month, SUM(`fdrtrans`.`in`) AS fdr
            FROM fdrtrans
            WHERE  (`fdrtrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
            AND (`fdrtrans`.`purpose` = 'FDR')
            GROUP BY MONTH(`fdrtrans`.`date`)
            ORDER BY `fdrtrans`.`date`

结果:

+-------+---------+
| month |    fdr  |
+-------+---------+
|   1   |  2000   |
|   2   |  750    |
|   3   |  200    |
|   4   |  180    |
|   5   |  570    |
|   6   |  625    |
+-------+---------+

我想要的是

+-------+---------+---------+
| month |    fdr  | loanout |
+-------+---------+---------+
|   1   |  2000   |  28000  |
|   2   |  750    |  27000  |
|   3   |  200    |  10200  |
|   4   |  180    |  7000   |
|   5   |  570    |  95000  |
|   6   |  625    |  2000   |
+-------+---------+---------+

如何获得这样的结果???

请查看此屏幕截图,了解我正在寻找的内容https://www.dropbox.com/s/kn8z7z4v7sbahf0/Capture4.PNG?dl=0 数据库:https://www.dropbox.com/s/8gbgrgvil915efr/bankdb.sql_7.zip?dl=0

4 个答案:

答案 0 :(得分:1)

您可以加入这两个查询。使用WITH不是必需的,但它极大地增强了查询对此类情况的可读性。

WITH q2 AS
(SELECT MONTH(`fdrtrans`.`date`) as month, SUM(`fdrtrans`.`in`) AS fdr
            FROM fdrtrans
            WHERE  (`fdrtrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
            AND (`fdrtrans`.`purpose` = 'FDR')
            GROUP BY MONTH(`fdrtrans`.`date`)
            ORDER BY `fdrtrans`.`date`)
, q1 AS
(SELECT MONTH(`loantrans`.`date`) as month, SUM(`loantrans`.`out`) AS loanout
            FROM loantrans
            WHERE  (`loantrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
            AND (`loantrans`.`purpose` = 'Loan')
            GROUP BY MONTH(`loantrans`.`date`)
            ORDER BY `loantrans`.`date`)
SELECT q1.month, q1.loanout, q2.fdr
FROM q1
JOIN q2 ON q1.month = q2.month
ORDER BY q1.month

答案 1 :(得分:0)

您必须使用JOIN语句将两个表连接在一起。请查看此网站,了解如何操作:

http://www.w3schools.com/sql/sql_join.asp

答案 2 :(得分:0)

这应该有所帮助:

SELECT s1.month,
       s1.loanout,
       s2.fdr
  FROM(SELECT MONTH(`loantrans`.`date`) as month, SUM(`loantrans`.`out`) AS loanout
         FROM loantrans
        WHERE  (`loantrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
          AND (`loantrans`.`purpose` = 'Loan')
     GROUP BY MONTH(`loantrans`.`date`           
) s1
 JOIN( SELECT MONTH(`fdrtrans`.`date`) as month, SUM(`fdrtrans`.`in`) AS fdr
         FROM fdrtrans
        WHERE  (`fdrtrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
          AND (`fdrtrans`.`purpose` = 'FDR')
     GROUP BY MONTH(`fdrtrans`.`date`)
) s2 ON s1.month = s2.month
ORDER BY 1

答案 3 :(得分:0)

在SQL Union中用于组合select查询的输出。

select * from loantrans
uninon
select * from fdrtrans