<i id='SaWII'><tr id='SaWII'><dt id='SaWII'><q id='SaWII'><span id='SaWII'><b id='SaWII'><form id='SaWII'><ins id='SaWII'></ins><ul id='SaWII'></ul><sub id='SaWII'></sub></form><legend id='SaWII'></legend><bdo id='SaWII'><pre id='SaWII'><center id='SaWII'></center></pre></bdo></b><th id='SaWII'></th></span></q></dt></tr></i><div id='SaWII'><tfoot id='SaWII'></tfoot><dl id='SaWII'><fieldset id='SaWII'></fieldset></dl></div>
      <bdo id='SaWII'></bdo><ul id='SaWII'></ul>

      <small id='SaWII'></small><noframes id='SaWII'>

    1. <tfoot id='SaWII'></tfoot>

      1. <legend id='SaWII'><style id='SaWII'><dir id='SaWII'><q id='SaWII'></q></dir></style></legend>

      2. 将几个查询的结果相加,然后在 SQL 中找到前 5 个

        Sum results of a few queries and then find top 5 in SQL(将几个查询的结果相加,然后在 SQL 中找到前 5 个)

        <legend id='4oXCr'><style id='4oXCr'><dir id='4oXCr'><q id='4oXCr'></q></dir></style></legend>
        • <small id='4oXCr'></small><noframes id='4oXCr'>

              <tbody id='4oXCr'></tbody>

              <bdo id='4oXCr'></bdo><ul id='4oXCr'></ul>
              <tfoot id='4oXCr'></tfoot>
                • <i id='4oXCr'><tr id='4oXCr'><dt id='4oXCr'><q id='4oXCr'><span id='4oXCr'><b id='4oXCr'><form id='4oXCr'><ins id='4oXCr'></ins><ul id='4oXCr'></ul><sub id='4oXCr'></sub></form><legend id='4oXCr'></legend><bdo id='4oXCr'><pre id='4oXCr'><center id='4oXCr'></center></pre></bdo></b><th id='4oXCr'></th></span></q></dt></tr></i><div id='4oXCr'><tfoot id='4oXCr'></tfoot><dl id='4oXCr'><fieldset id='4oXCr'></fieldset></dl></div>
                  本文介绍了将几个查询的结果相加,然后在 SQL 中找到前 5 个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有 3 个查询:

                  table: pageview
                  SELECT event_id, count(*) AS pageviews 
                  FROM pageview 
                  GROUP BY event_id
                  ORDER BY pageviews DESC, rand()
                  LIMIT 1000
                  
                  table: upvote
                  SELECT event_id, count(*) AS upvotes 
                  FROM upvote
                  GROUP BY event_id
                  ORDER BY upvotes DESC, rand()
                  LIMIT 1000
                  
                  table: attending
                  SELECT event_id, count(*) AS attendants
                  FROM attending
                  GROUP BY event_id
                  ORDER BY attendants DESC, rand()
                  LIMIT 1000
                  

                  我想合并按金额排序的所有 3 个查询的 event_id,然后选择前 5 个.我该怎么做?

                  I'd like to combine the event_ids of all 3 queries ordered by amount and then choose the top 5. How do I do that?

                  这是我为让它发生而做的:

                  SELECT event_id, sum(amount) AS total
                  FROM (
                  (SELECT event_id, count(*) AS amount
                  FROM   pageview 
                  GROUP  BY event_id
                  ORDER  BY amount DESC, rand()
                  LIMIT  1000)
                  
                  UNION ALL
                  (SELECT event_id, count(*) as amount
                  FROM   upvote
                  GROUP  BY event_id
                  ORDER  BY amount DESC, rand()
                  LIMIT  1000)
                  
                  UNION ALL
                  (SELECT event_id, count(*) as amount
                  FROM   attending
                  GROUP  BY event_id
                  ORDER  BY amount DESC, rand()
                  LIMIT  1000)
                  ) x
                  GROUP  BY 1
                  ORDER  BY  sum(amount) DESC
                  LIMIT  5;
                  

                  推荐答案

                  将所有三个查询的结果行UNION,然后选择amount最高的5行:

                  To UNION the resulting rows of all three queries and then pick the 5 rows with the highest amount:

                  (SELECT event_id, count(*) AS amount
                  FROM   pageview 
                  GROUP  BY event_id
                  ORDER  BY pageviews DESC, rand()
                  LIMIT  1000)
                  
                  UNION ALL
                  (SELECT event_id, count(*)
                  FROM   upvote
                  GROUP  BY event_id
                  ORDER  BY upvotes DESC, rand()
                  LIMIT  1000)
                  
                  UNION ALL
                  (SELECT event_id, count(*)
                  FROM   attending
                  GROUP  BY event_id
                  ORDER  BY attendants DESC, rand()
                  LIMIT  1000)
                  
                  ORDER  BY 2 DESC
                  LIMIT  5;
                  

                  手册:

                  要将 ORDER BYLIMIT 应用于单个 SELECT,请将括在 SELECT 的括号内的子句.

                  To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT.

                  UNION ALL 保留重复项.

                  为每个event_id添加计数:

                  To add the counts for every event_id:

                  SELECT event_id, sum(amount) AS total
                  FROM (
                     (SELECT event_id, count(*) AS amount
                      FROM   pageview 
                      GROUP  BY event_id
                      ORDER  BY pageviews DESC, rand()
                      LIMIT  1000)
                      
                      UNION ALL
                      (SELECT event_id, count(*)
                      FROM   upvote
                      GROUP  BY event_id
                      ORDER  BY upvotes DESC, rand()
                      LIMIT  1000)
                      
                      UNION ALL
                      (SELECT event_id, count(*)
                      FROM   attending
                      GROUP  BY event_id
                      ORDER  BY attendants DESC, rand()
                      LIMIT  1000)
                      ) x
                  GROUP  BY 1
                  ORDER  BY sum(amount) DESC
                  LIMIT  5;
                  

                  这里的棘手部分是并非每个 event_id 都会出现在所有三个基本查询中.所以要注意 JOIN 不会完全丢失行并且添加不会变成 NULL.

                  The tricky part here is that not every event_id will be present in all three base queries. So take care that a JOIN does not lose rows completely and additions don't turn out NULL.

                  使用UNION ALL,而不是UNION.您不想删除相同的行,而是想将它们相加.

                  Use UNION ALL, not UNION. You don't want to remove identical rows, you want to add them up.

                  xAS x 的表别名和简写.子查询必须具有名称.此处可以是任何其他名称.

                  x is a table alias and shorthand for AS x. It is required for for a subquery to have a name. Can be any other name here.

                  SOL 特性 FULL OUTER JOIN 在 MySQL 中没有实现(我上次检查过),所以你必须使用 UNION.FULL OUTER JOIN 将连接所有三个基本查询而不会丢失行.

                  The SOL feature FULL OUTER JOIN is not implemented in MySQL (last time I checked), so you have to make do with UNION. FULL OUTER JOIN would join all three base queries without losing rows.

                  SELECT event_id, sum(amount) AS total
                  FROM (
                     (SELECT event_id, count(*) / 100 AS amount
                      FROM   pageview ... )
                      
                      UNION ALL
                      (SELECT event_id, count(*) * 5 
                      FROM   upvote ... )
                      
                      UNION ALL
                      (SELECT event_id, count(*) * 10
                      FROM   attending ... )
                      ) x
                  GROUP  BY 1
                  ORDER  BY  sum(amount) DESC
                  LIMIT  5;
                  

                  或者,以多种方式使用基本计数:

                  Or, to use the base counts in multiple ways:

                  SELECT event_id
                        ,sum(CASE source
                                WHEN 'p' THEN amount / 100
                                WHEN 'u' THEN amount * 5
                                WHEN 'a' THEN amount * 10
                                ELSE 0
                             END)  AS total
                  FROM (
                     (SELECT event_id, 'p'::text AS source, count(*) AS amount
                      FROM   pageview ... )
                      
                      UNION ALL
                      (SELECT event_id, 'u'::text, count(*)
                      FROM   upvote ... )
                      
                      UNION ALL
                      (SELECT event_id, 'a'::text, count(*)
                      FROM   attending ... )
                      ) x
                  GROUP  BY 1
                  ORDER  BY 2 DESC
                  LIMIT  5;
                  

                  这篇关于将几个查询的结果相加,然后在 SQL 中找到前 5 个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to update a record using sequelize for node?(如何使用节点的 sequelize 更新记录?)
                  How to provide a mysql database connection in single file in nodejs(如何在 nodejs 中的单个文件中提供 mysql 数据库连接)
                  Looping Over Result Sets in MySQL(在 MySQL 中循环结果集)
                  How to get Top 5 records in SqLite?(如何获得 Sqlite 中的前 5 条记录?)
                  SQL Server SELECT where any column contains #39;x#39;(SQL Server SELECT,其中任何列包含“x)
                  Default row order in SELECT query - SQL Server 2008 vs SQL 2012(SELECT 查询中的默认行顺序 - SQL Server 2008 与 SQL 2012)
                    <bdo id='KF0FZ'></bdo><ul id='KF0FZ'></ul>

                      • <small id='KF0FZ'></small><noframes id='KF0FZ'>

                          <tbody id='KF0FZ'></tbody>
                      • <tfoot id='KF0FZ'></tfoot>

                          <legend id='KF0FZ'><style id='KF0FZ'><dir id='KF0FZ'><q id='KF0FZ'></q></dir></style></legend>
                        • <i id='KF0FZ'><tr id='KF0FZ'><dt id='KF0FZ'><q id='KF0FZ'><span id='KF0FZ'><b id='KF0FZ'><form id='KF0FZ'><ins id='KF0FZ'></ins><ul id='KF0FZ'></ul><sub id='KF0FZ'></sub></form><legend id='KF0FZ'></legend><bdo id='KF0FZ'><pre id='KF0FZ'><center id='KF0FZ'></center></pre></bdo></b><th id='KF0FZ'></th></span></q></dt></tr></i><div id='KF0FZ'><tfoot id='KF0FZ'></tfoot><dl id='KF0FZ'><fieldset id='KF0FZ'></fieldset></dl></div>