简体   繁体   English

MySQL查询GROUP BY两列

[英]MySQL query GROUP BY two columns

I need help with MySQL query 我需要有关MySQL查询的帮助
I have this cenary: 我有这个场景:

table A 表A
- page_id -page_id
- rev_id -rev_id
- date -日期
one page_id can have multiples rev_id 一个page_id可以有多个rev_id

table B 表B
- rev_id -rev_id
- words -字
I have what words have in each revision 我每个修订版都有什么用

I need return for each date the quantity of words that I have in the last rev_id in each page_id 我需要为每个日期返回每个page_id中上一个rev_id中包含的单词数量

Example: 例:

table A
page_id | rev_id   |   date
---------------------------------
 1      |  231     |   2002-01-01
 2      |  345     |   2002-10-12
 1      |  324     |   2002-10-13
 3      |  348     |   2003-01-01

-- -

table B
rev_id | words
---------------
231    | 'ask'
231    | 'the'
231    | 'if'
345    | 'ask'
324    | 'ask'
324    | 'if'
348    | 'who'

magical sql here edited to show how its calculated {page_id : [words]} 此处编辑了神奇的sql,以显示其计算方式{page_id:[words]}

date        |  count(words)
--------------------------
2002-01-01  |    3           { 1:[ask, the, if] }
2002-10-12  |    4           { 1:[ask, the, if], 2:[ask] }
2002-10-13  |    3           { 1:[ask, if], 2:[ask] }
2003-01-01  |    4           { 1:[ask, if], 2:[ask], 3:[who] }

I did this query, but my date are fixed and I need for all dates contained in table revision: 我做了这个查询,但是我的日期是固定的,我需要表修订中包含的所有日期:

SELECT SUM(q) 
FROM (
    SELECT COUNT(equation) q 
    FROM revision r, equation e
    WHERE r.rev_id in (
        SELECT max(rev_id) 
        FROM revision 
        WHERE date < '2006-01-01' 
        GROUP BY page_id
    ) 
    AND r.rev_id = e.rev_id
    GROUP BY date
) q;

Solved 解决了

My friend help-me to create query to solve my problem! 我的朋友帮助我创建查询来解决我的问题!

select s.date, count(words) from
(select d.date, r.page_id, max(r.rev_id) as rev_id 
    from revision r, (select distinct(date) from revision) d 
    where d.date >= r.date group by d.date, r.page_id) s
join words e on e.rev_id = s.rev_id
group by s.date;

I think this is a basic join and group by : 我认为这是一个基本的joingroup by

select a.date, count(*)
from a join
     b
     on a.rev_id = b.rev_id
group by a.date;

EDIT: 编辑:

Oh, I think I get it. 哦,我想我明白了。 This is a cumulative thing. 这是累积的事情。 That makes it more complicated. 这使其变得更加复杂。

select d.date,
       (select count(*)
        from a join
             b
             on a.rev_id = b.rev_id
        where a.date <= d.date and
              a.rev_id = (select max(a2.rev_id) from a a2 where a2.date = a.date and a2.date <= d.date)
       ) as cnt
from (select date from a) d;

But that won't work in MySQL because of the nesting of the correlation clause. 但是由于相关子句的嵌套,在MySQL中这是行不通的。 So, we can restructure the logic as: 因此,我们可以将逻辑重构为:

select a.date, count(*)
from (select a.*,
             (select max(a2.rev_id)
              from a a2
              where a2.date <= a.date and a2.page_id = a.page_id
             ) as last_rev_id
      from a
     ) a join
     b
     on a.last_rev_id = b.rev_id
group by a.date;

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

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