如何将2个查询结果连接在一起?

时间:2014-09-19 19:16:47

标签: mysql sql

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

详细说明: 表: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  |
+-----+----------+---------+---------+---------+

查询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(`loantrans`.`date`) as month, SUM(`loantrans`.`out`) AS intout
            FROM loantrans
            WHERE  (`loantrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
            AND (`loantrans`.`purpose` = 'Interest')
            GROUP BY MONTH(`loantrans`.`date`)
            ORDER BY `loantrans`.`date`

结果:

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

我想要的是

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

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

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

5 个答案:

答案 0 :(得分:2)

  SELECT MONTH(lt.date) month, 
         SUM((lt.purpose='Interest')*lt.out) intout,
         SUM((lt.purpose='Loan')*lt.out) loanout
    FROM loantrans lt
   WHERE lt.date BETWEEN '2014-01-01' AND '2014-09-20'
  /* AND lt.purpose IN ('Interest', 'Loan') /* If you have more purposes. */  
GROUP BY month
ORDER BY month

答案 1 :(得分:1)

 SELECT MONTH(lt.date) month, 
         SUM( case when lt.purpose='Interest' then lt.out else 0 end) intout,
         SUM(case when lt.purpose='Loan' then lt.out else 0 end) loanout
    FROM loantrans lt
   WHERE lt.date BETWEEN '2014-01-01' AND '2014-09-20'
GROUP BY month
ORDER BY month

http://sqlfiddle.com/#!2/fa8ac/5

答案 2 :(得分:0)

将此用于If语句

 SELECT MONTH(l.date) month, 
     SUM(if (l.purpose='Interest' ,l.out , 0) ) intout,
     SUM(if (l.purpose='Loan', l.out ,0 )) loanout
 FROM loantrans l
 WHERE l.date BETWEEN '2014-01-01' AND '2014-09-20'
GROUP BY month
ORDER BY month

答案 3 :(得分:0)

可能更好的方法,但我认为这有效:

select  coalesce(tblOne.month, tblTwo.month), intOut, loanOut
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`)
) tblOne
left join (
        SELECT MONTH(`loantrans`.`date`) as month, SUM(`loantrans`.`out`) AS intout
        FROM loantrans
        WHERE  (`loantrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
        AND (`loantrans`.`purpose` = 'Interest')
        GROUP BY MONTH(`loantrans`.`date`)
) tblTwo on tblOne.month = tblTwo.month
order by month

答案 4 :(得分:0)

SELECT l1.month, l1.loanout, l2.intout 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`)
        ORDER BY `loantrans`.`date`
   ) AS l1 JOIN (
    SELECT MONTH(`loantrans`.`date`) as month, SUM(`loantrans`.`out`) AS intout
        FROM loantrans
        WHERE  (`loantrans`.`date` BETWEEN '2014-01-01' AND '2014-09-20')
        AND (`loantrans`.`purpose` = 'Interest')
        GROUP BY MONTH(`loantrans`.`date`)
        ORDER BY `loantrans`.`date`
   ) AS l2 ON l1.month = l2.month;