将1列mysql结果添加到另一个mysql结果的结果中

时间:2012-04-17 15:11:08

标签: php mysql

目前我有两个不同的mysql查询:

查询1

SELECT monthname( calendar.datefield ) AS date,
    year(calendar.datefield) as year, calendar.datefield, COUNT(all_griefs_tbl.actioned_status ) AS total_griefs,
    all_griefs_tbl.actioned_status, all_griefs_tbl.game
FROM all_griefs_tbl 
RIGHT JOIN calendar 
  ON ( DATE(all_griefs_tbl.actioned_date ) = calendar.datefield )
    AND all_griefs_tbl.actioned_status = 'accepted'
WHERE calendar.datefield
  BETWEEN DATE_ADD(CURDATE(), INTERVAL -12 MONTH)  AND CURDATE()
GROUP BY year( calendar.datefield ) DESC , month( calendar.datefield ) DESC

查询2

SELECT monthname( calendar.datefield ) AS date, 
    year(calendar.datefield ) AS year, calendar.datefield,
    COUNT(all_griefs_tbl.actioned_status ) AS total_submitted,
    all_griefs_tbl.actioned_status, all_griefs_tbl.game 
FROM all_griefs_tbl 
RIGHT JOIN calendar 
  ON ( DATE( all_griefs_tbl.date ) = calendar.datefield ) 
WHERE calendar.datefield BETWEEN DATE_ADD( CURDATE( ) , INTERVAL -12 MONTH ) AND CURDATE( ) 
GROUP BY year( calendar.datefield ) DESC , month( calendar.datefield ) DESC

现在它们之间的区别在于查询1我计算每月接受的悲伤数量,在查询2中我计算每月提交的记录数 - 计算不同的列

我想要做的是a)将其转换为单个查询或b)能够将结果合并到1个表中。

我希望输出如下:

    Month         Year          Total Griefs  Total Submitted
    ------------  ------------  ------------  ------------  
    April         2012          14            2      
    March         2012          0             8
    February      2012          0             6
    January       2012          0             13
    December      2011          0             7
    November      2011          0             10
    October       2011          0             0
    September     2011          0             0
    August        2011          0             6
    July          2011          0             3
    June          2011          0             2
    May           2011          0             0
    April         2011          0             0

这可能吗,还是我完全咆哮错误的树?

谢谢!

3 个答案:

答案 0 :(得分:3)

您可以在一个查询中执行此操作:

SELECT 
    monthname( calendar.datefield ) AS date,
    year(calendar.datefield) as year, 
    calendar.datefield,
    SUM(IF(all_griefs_tbl.actioned_status = 'accepted', 1, 0)) AS total_griefs,
    COUNT(all_griefs_tbl.actioned_status ) AS total_submitted,
    all_griefs_tbl.actioned_status, 
    all_griefs_tbl.game 
FROM 
    all_griefs_tbl 
RIGHT JOIN 
    calendar ON ( DATE(all_griefs_tbl.actioned_date ) = calendar.datefield ) 
WHERE 
    calendar.datefield BETWEEN DATE_ADD(CURDATE(), INTERVAL -12 MONTH)  AND CURDATE() 
GROUP BY year( calendar.datefield ) DESC , 
    month( calendar.datefield ) DESC

编辑:这有点不正统,但它(很有可能)会给你你想要的东西:

SELECT
    month(date_add(curdate(), interval seq.mm months)),
    year(date_add(curdate(), interval seq.mm months)),
    (SELECT count(1)
       FROM all_griefs_tbl
      WHERE actioned_status = 'accepted'
        AND month(action_date) = month(date_add(curdate(), interval seq.mm months))
        AND year(action_date) = year(date_add(curdate(), interval seq.mm months))) as total_griefs
    (SELECT count(1)
       FROM all_griefs_tbl
        AND month(date) = month(date_add(curdate(), interval seq.mm months))
        AND year(date) = year(date_add(curdate(), interval seq.mm months))) as submitted_griefs
FROM
    (SELECT -12 as mm UNION SELECT -11  as mmUNION SELECT -10  as mmUNION SELECT -9  as mm
     UNION SELECT -8 as mm UNION SELECT -7  as mm UNION SELECT -6 as mm UNION SELECT -5 as mm
     UNION SELECT -4 as mm UNION SELECT -3 as mm UNION SELECT -2 as mm UNION SELECT -1 as mm) seq
ORDER BY
    mm

答案 1 :(得分:0)

如果输出与2个查询相同,则可以对查询使用UNION

因此,请在查询中添加NULL AS total_griefs,NULL AS total_submitted,,以便输出与

匹配

答案 2 :(得分:0)

试试这个(我真的无法测试)

SELECT monthname(calendar.datefield) AS date, 
       year(calendar.datefield) as year, 
       calendar.datefield, 
       SUM(CASE all_griefs_tbl.actioned_status 
               WHEN 'accepted' THEN 1 
               ELSE 0 
           END CASE) AS total_griefs,
       COUNT(all_griefs_tbl.actioned_status) AS total_submitted,
       all_griefs_tbl.actioned_status, 
       all_griefs_tbl.game 
FROM all_griefs_tbl RIGHT JOIN calendar 
    ON DATE(all_griefs_tbl.actioned_date) = calendar.datefield 
WHERE calendar.datefield 
    BETWEEN DATE_ADD(CURDATE(), INTERVAL -12 MONTH) AND CURDATE() 
GROUP BY year(calendar.datefield) DESC, 
         month(calendar.datefield) DESC