doumibi6899 2014-12-19 09:58
浏览 15
已采纳

根据lastmodified column比较两个表?

I am using this query to check tickets group by month and year

SELECT 
    MONTH((lastmodified)) as month, 
    YEAR((lastmodified)) as year,
    COUNT(num_of_tickets) as tickets
    FROM visitors_2013
    GROUP BY 
    MONTH((lastmodified)), 
    YEAR((lastmodified))

this return something like

Array
(
    [0] => Array
        (
            [month] => 1
            [year] => 2013
            [tickets] => 1521
        )

    [1] => Array
        (
            [month] => 2
            [year] => 2013
            [tickets] => 513
        )

    [2] => Array
        (
            [month] => 12
            [year] => 2012
            [tickets] => 146
        )

)

I have put this in a php function so i can get the data per year (data is stored in a seperate table per year). I call function get_ticket_per_year($year) twice for 2013 and 2014

Array
(
    [0] => Array
        (
            [month] => 1
            [year] => 2013
            [tickets] => 1521
        )

    [1] => Array
        (
            [month] => 2
            [year] => 2013
            [tickets] => 513
        )

    [2] => Array
        (
            [month] => 12
            [year] => 2012
            [tickets] => 146
        )

)

Array
(
    [0] => Array
        (
            [month] => 12
            [year] => 2013
            [tickets] => 26
        )

)

And i like to compare month-year-total tickets from another table (visitors_2014) by plotting the data per month per year in a graph. For this i am using the morris js library. This is the outcome i need

data: [ {
            m: 'dec',
            2012: 146,
            2013: 26
        }, {
            m: 'jan',
            2013: 1521,
            2014: 
        }],

So how can i combine the mysql query so it's one query

SELECT 
prevTbl.MONTH((lastmodified)) as prev_month, 
prevTbl.YEAR((lastmodified)) as prev_year,
prevTbl.COUNT(num_of_tickets) as prev_tickets,
currTbl.MONTH((lastmodified)) as curr_month, 
currTbl.YEAR((lastmodified)) as curr_year,
currTbl.COUNT(num_of_tickets) as curr_tickets
FROM visitors_2013 prevTbl, visitors_2014 currTbl 
GROUP BY 
MONTH((lastmodified)), 
YEAR((lastmodified))
  • 写回答

1条回答 默认 最新

  • doutonghang2761 2014-12-19 10:12
    关注

    Join your queries:

    SELECT * FROM ( [your first query here] ) AS data1
    LEFT JOIN ( [your second query here] ) AS data2
    ON [your relation between the two query here]
    

    In your case (not tested):

    SELECT * FROM (
    
        SELECT
            `num_of_tickets` AS `tickets2013`,
            MONTH(`lastmodified`) AS `month`
        FROM `visitors_2013`
        GROUP BY 
            MONTH(`lastmodified`)
    
    ) AS `data2013`
    LEFT JOIN (
    
        SELECT
            `num_of_tickets` AS `tickets2014`,
            MONTH(`lastmodified`) AS `month`
        FROM `visitors_2014`
        GROUP BY 
            MONTH(`lastmodified`)
    
    ) AS `data2014`
    ON (
        `data2013`.`month`=`data2014`.`month`
    )
    

    Why do not you store your (actively used) data in the same table (much more simpler)?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示