简体   繁体   English

mySQL子查询

[英]mySQL subqueries

i wrote this code which has the following result and now I want to sum the result of the two rows obtained after the union. 我写的这段代码具有以下结果,现在我想对联合后获得的两行的结果求和。 Any idea how can i do that (i mean i need another select on this new "table"). 任何想法我该怎么做(我的意思是我需要在这个新的“表”上进行另一个选择)。 Any help is appreciated 任何帮助表示赞赏

SELECT Date_format(`creation`, '%Y-%m-%d') AS short_date,
       Sum(`param7`)                       AS time_spent_in_sessions,
       Count(*)                            AS nb_of_sessions
FROM   `f_colony_ios_play_resume_pn__20120823`
WHERE  `id` IN ( '22965', '22966' )

UNION

SELECT Date_format(`creation`, '%Y-%m-%d') AS short_date,
       Sum(`param8`)                       AS time_spent_in_sessions,
       Count(*)                            AS nb_of_sessions
FROM   `f_colony_ios_play_resume_pn__20120823`
WHERE  `id` IN ( '22970', '22971' )

-----------------------------
result:
    short_date       time_spent_in_sessions      nb_of_sessions
    2012-08-23               7                        2
    2012-08-23               10                       1
SELECT short_date, SUM(time_spent_in_sessions), SUM(nb_of_sessions) 
FROM (both your queries here) AS subquerytable

I think this is the way to go... 我认为这是要走的路...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM