简体   繁体   English

已加入并计数的mysql查询

[英]mysql query that has join and counts

I need help getting the top 5 results and their counts from columns from two different tables in a mysql database joined together. 我需要帮助,以将MySQL数据库中两个不同表的列中的前5个结果及其计数合并在一起。

table1 cols
-------
id, country, timestamp

table2 cols
--------
id, table1_id, reason

The results id like to get are the top 5 countries and their number of times found between two timestamps, and the top 5 reasons and their counts for all the rows used to generate the first count. 想要获取的结果ID是前5个国家/地区及其在两个时间戳之间找到的时间,以及前5个国家/地区及其产生第一个计数的所有行的计数。 There is a one to many relationship between table1 and table2. table1和table2之间存在一对多关系。 This is stumping me and I appreciate any insight you could give me. 这让我很沮丧,我感谢您能提供的任何见解。

It's not entirely clear what resultset you want to return. 尚不清楚要返回什么结果集。

This may be of some help to you: 这可能对您有所帮助:

SELECT t.country
     , COUNT(DISTINCT t.id) AS count_table1_rows
     , COUNT(r.id)          AS count_table2_rows
     , COUNT(*)             AS count_total_rows
  FROM table1 t
  LEFT
  JOIN table2 r
    ON r.table1_id = t.id
 WHERE t.timestamp >= NOW() - INTERVAL 7 DAY
   AND t.timestamp  < NOW()
 GROUP BY t.country
 ORDER BY COUNT(DISTINCT t.id) DESC
 LIMIT 5

That will return a maximum of 5 rows, one row per country, with counts of rows in table1, counts of rows found in table2, and a count of the total rows returned. 这将最多返回5行,每个国家/地区返回一行,其中table1中的行数,table2中找到的行数以及返回的总行数。

The LEFT keyword specifies an "outer" join operation, such that rows from table1 are returned even if there are no matching rows found in table2. LEFT关键字指定“外部”连接操作,这样即使在table2中找不到匹配的行,也将返回来自table1的行。

To get the count for each "reason", associated with each country, you could do something like this: 要获得与每个国家/地区相关的每个“原因”的计数,您可以执行以下操作:

SELECT t.country
     , COUNT(DISTINCT t.id) AS count_table1_rows
  FROM table1 t
  LEFT
  JOIN ( SELECT s.country
              , r.reason
              , COUNT(*) AS cnt_r
           FROM table1 s 
           JOIN table2 r
             ON s.table1_id = t.id
          WHERE s.timestamp >= NOW() - INTERVAL 7 DAY
            AND s.timestamp  < NOW()
          GROUP
             BY s.country
              , r.reason
       ) u
    ON u.country = t.country 
 WHERE t.timestamp >= NOW() - INTERVAL 7 DAY
   AND t.timestamp  < NOW()
 GROUP
    BY t.country
     , u.reason
 ORDER
    BY COUNT(DISTINCT t.id) DESC
     , t.country DESC
     , u.cnt_r DESC
     , u.reason DESC

This query doesn't "limit" the rows being returned. 该查询不会“限制”返回的行。 It would be possible to modify the query to have only a subset of the rows returned, but that can get complex. 可以将查询修改为仅返回部分行,但这会变得很复杂。 And before we muck the complexity of adding "top 5 within top 5" type limits, we want to ensure that the rows returned by a query are a superset of the rows we actually want. 在弄清添加“前5名内的前5名”类型限制的复杂性之前,我们要确保查询返回的行是我们实际想要的行的超集。

Is this what you want? 这是你想要的吗?

select t2.reason, count(*)
from (select t1.country, count(*)
      from table1 t1
      where timestamp between @STARTTIME and @ENDTIME
      group by country
      order by count(*) desc
      limit 5
     ) c5 join
     table1 t1
     on c5.country = t1.country and
        t1.timestamp between @STARTTIME and @ENDTIME join
     table2 t2
     on t2.table1_id = t1.id
group by t2.reason;

The c5 subquery gets the five countries. c5子查询获取五个国家。 The other two bring back the data for the final aggregation. 其他两个带回数据以进行最终聚合。

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

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