• <legend id='ONk4L'><style id='ONk4L'><dir id='ONk4L'><q id='ONk4L'></q></dir></style></legend>
    1. <small id='ONk4L'></small><noframes id='ONk4L'>

        <bdo id='ONk4L'></bdo><ul id='ONk4L'></ul>

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

        MySql 查询:从表中为每个类别选择前 3 行

        时间:2023-06-01
        <legend id='BawwB'><style id='BawwB'><dir id='BawwB'><q id='BawwB'></q></dir></style></legend>

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

                <tbody id='BawwB'></tbody>

              • <bdo id='BawwB'></bdo><ul id='BawwB'></ul>

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

                1. 本文介绍了MySql 查询:从表中为每个类别选择前 3 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个包含记录的表,其中有一行名为 category.我插入了太多文章,我只想从每个类别中选择两篇文章.

                  I have a table with records and it has a row called category. I have inserted too many articles and I want to select only two articles from each category.

                  我试图做这样的事情:

                  我创建了一个视图:

                  CREATE VIEW limitrows AS 
                     SELECT * FROM tbl_artikujt ORDER BY articleid DESC LIMIT 2 
                  

                  然后我创建了这个查询:

                  Then I created this query:

                  SELECT * 
                  FROM tbl_artikujt 
                  WHERE 
                     artikullid IN
                     (
                        SELECT artikullid
                        FROM limitrows
                        ORDER BY category DESC
                     )
                  ORDER BY category DESC;
                  

                  但这不起作用并且只给我两条记录?

                  But this is not working and is giving me only two records?

                  推荐答案

                  LIMIT 只停止语句返回的结果数.您正在寻找的通常称为分析/窗口/排名函数 - MySQL 不支持,但您可以使用变量进行模拟:

                  LIMIT only stops the number of results the statement returns. What you're looking for is generally called analytic/windowing/ranking functions - which MySQL doesn't support but you can emulate using variables:

                  SELECT x.*
                    FROM (SELECT t.*,
                                 CASE 
                                   WHEN @category != t.category THEN @rownum := 1 
                                   ELSE @rownum := @rownum + 1 
                                 END AS rank,
                                 @category := t.category AS var_category
                            FROM TBL_ARTIKUJT t
                            JOIN (SELECT @rownum := NULL, @category := '') r
                        ORDER BY t.category) x
                   WHERE x.rank <= 3
                  

                  如果您不更改 SELECT x.*,结果集将包括 rankvar_category 值 - 您将拥有如果不是这种情况,请指定您真正想要的列.

                  If you don't change SELECT x.*, the result set will include the rank and var_category values - you'll have to specify the columns you really want if this isn't the case.

                  这篇关于MySql 查询:从表中为每个类别选择前 3 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Mysql 权限错误与“加载数据" 下一篇:如何根据关系获取针对一条记录的多条记录?

                  相关文章

                2. <small id='R0eQG'></small><noframes id='R0eQG'>

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

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

                        <bdo id='R0eQG'></bdo><ul id='R0eQG'></ul>