对页面进行分组并从每组中获取 x 行数

发布于 2024-12-11 06:57:01 字数 6925 浏览 0 评论 0原文

根据我之前提出的 问题,我进行了下面的查询,但它不能限制每组中一定数量的行,

SELECT *
  FROM (SELECT p.page_id AS page_id,
               p.page_url AS page_url,
           SUBSTRING(p.page_title,LOCATE('{',p.page_title),LOCATE('}',p.page_title)) AS group_title,
               p.page_created AS page_created,
               @row_number := @row_number + 1 AS row_number
          FROM root_pages AS p
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_backdate DESC) a
 WHERE row_number <= 2

结果,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   NULL
43      def         {a}         2011-10-21...   NULL
42      qwe         {b}         2011-10-21...   NULL
41      rty         {b}         2011-10-21...   NULL
40      tyu         {c}         2011-10-21...   NULL
39      ghj         {c}         2011-10-21...   NULL
59      sss         {c}         2011-10-21...   NULL

但我想得到这个结果,

结果,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   1
43      def         {a}         2011-10-21...   2
42      qwe         {b}         2011-10-21...   1
41      rty         {b}         2011-10-21...   2
40      tyu         {c}         2011-10-21...   1
39      ghj         {c}         2011-10-21...   2

知道我错过了什么吗?

编辑1:

查询,

SELECT a.*
  FROM (SELECT p.page_id,
               p.page_url,
               SUBSTRING(p.page_title, LOCATE('{',p.page_title),LOCATE('}', p.page_title)) AS group_title,
               p.page_created,
               @row_number := @row_number + 1 AS row_number
          FROM root_pages AS p
          JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_backdate DESC) a

结果,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   1
43      def         {a}         2011-10-21...   2
42      qwe         {b}         2011-10-21...   3
41      rty         {b}         2011-10-21...   4
40      tyu         {c}         2011-10-21...   5
39      ghj         {c}         2011-10-21...   6
59      sss         {c}         2011-10-21...   7

查询,

SELECT a.*
  FROM (SELECT p.page_id,
               p.page_url,
               SUBSTRING(p.page_title, LOCATE('{',p.page_title),LOCATE('}', p.page_title)) AS group_title,
               p.page_created,
               @row_number := @row_number + 1 AS row_number
          FROM root_pages AS p
          JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_backdate DESC) a
WHERE a.row_number <= 2

结果,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   1
43      def         {a}         2011-10-21...   2

查询,

SELECT *

FROM(
    SELECT 

        p.page_id AS page_id,
        p.page_url AS page_url,
        SUBSTRING(p.page_title,LOCATE('{',p.page_title),LOCATE('}',p.page_title)) AS group_title,
        p.page_created AS page_created,
         @url = p.page_url,
               @row_number := CASE 
                                WHEN p.page_url = @url THEN @row_number + 1 
                                ELSE 1
                              END AS row_number

    FROM root_pages AS p
    JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable

    WHERE p.parent_id = '8'
    AND p.page_id != '8'
    AND p.page_hide != '1'
    AND p.category_id = '1'

    ORDER BY p.page_backdate DESC


) a 
where  row_number <= 2

结果,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   1
43      def         {a}         2011-10-21...   1
42      qwe         {b}         2011-10-21...   1
41      rty         {b}         2011-10-21...   1
40      tyu         {c}         2011-10-21...   1
39      ghj         {c}         2011-10-21...   1
59      sss         {c}         2011-10-21...   1

编辑2:

查询,

SELECT 
    a.*,
    @r := CASE WHEN @g = a.group_title THEN @r+1 ELSE 1 END AS r,
    @g := a.group_title AS dummy

FROM(
    SELECT 

        p.page_id AS page_id,
        p.page_url AS page_url,
        SUBSTRING(p.page_title,LOCATE('{',p.page_title),LOCATE('}',p.page_title)) AS group_title,
        p.page_created AS page_created

    FROM root_pages AS p

    WHERE p.parent_id = '8'
    AND p.page_id != '8'
    AND p.page_hide != '1'
    AND p.category_id = '1'

    ORDER BY p.page_backdate DESC

) a

JOIN (SELECT @r := 0, @g) r -- Initialize the variable

结果,

page_id page_url    group_title page_created    dummy       row_number
44      abc         {a}         2011-10-21...   {a}                 1
43      def         {a}         2011-10-21...   {a}                 1
42      qwe         {b}         2011-10-21...   {b}                 1
41      rty         {b}         2011-10-21...   {b}                 1
40      tyu         {c}         2011-10-21...   {c}                 1
39      ghj         {c}         2011-10-21...   {c}                 1
59      sss         {c}         2011-10-21...   {c}                 1

答案:

SELECT *

FROM
(
    SELECT 
        a.*,
        @r := CASE WHEN @g = a.group_title THEN @r+1 ELSE 1 END AS r,
        @g := a.group_title AS dummy

    FROM(
        SELECT 

            p.page_id AS page_id,
            p.page_url AS page_url,
            SUBSTRING(p.page_title,LOCATE('{',p.page_title),LOCATE('}',p.page_title)) AS group_title,
            p.page_created AS page_created

        FROM root_pages AS p

        WHERE p.parent_id = '8'
        AND p.page_id != '8'
        AND p.page_hide != '1'
        AND p.category_id = '1'

        ORDER BY p.page_backdate DESC

    ) a

    JOIN (SELECT @g:=null, @r:= 0) n -- Initialize the variable
) b
WHERE r <= 2

Following up from the question I made earlier, I made the query below but it cannot limit certain number of row from each group,

SELECT *
  FROM (SELECT p.page_id AS page_id,
               p.page_url AS page_url,
           SUBSTRING(p.page_title,LOCATE('{',p.page_title),LOCATE('}',p.page_title)) AS group_title,
               p.page_created AS page_created,
               @row_number := @row_number + 1 AS row_number
          FROM root_pages AS p
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_backdate DESC) a
 WHERE row_number <= 2

result,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   NULL
43      def         {a}         2011-10-21...   NULL
42      qwe         {b}         2011-10-21...   NULL
41      rty         {b}         2011-10-21...   NULL
40      tyu         {c}         2011-10-21...   NULL
39      ghj         {c}         2011-10-21...   NULL
59      sss         {c}         2011-10-21...   NULL

But I want to get this as the result,

result,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   1
43      def         {a}         2011-10-21...   2
42      qwe         {b}         2011-10-21...   1
41      rty         {b}         2011-10-21...   2
40      tyu         {c}         2011-10-21...   1
39      ghj         {c}         2011-10-21...   2

Any idea where I have missed?

EDIT 1:

Query,

SELECT a.*
  FROM (SELECT p.page_id,
               p.page_url,
               SUBSTRING(p.page_title, LOCATE('{',p.page_title),LOCATE('}', p.page_title)) AS group_title,
               p.page_created,
               @row_number := @row_number + 1 AS row_number
          FROM root_pages AS p
          JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_backdate DESC) a

result,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   1
43      def         {a}         2011-10-21...   2
42      qwe         {b}         2011-10-21...   3
41      rty         {b}         2011-10-21...   4
40      tyu         {c}         2011-10-21...   5
39      ghj         {c}         2011-10-21...   6
59      sss         {c}         2011-10-21...   7

Query,

SELECT a.*
  FROM (SELECT p.page_id,
               p.page_url,
               SUBSTRING(p.page_title, LOCATE('{',p.page_title),LOCATE('}', p.page_title)) AS group_title,
               p.page_created,
               @row_number := @row_number + 1 AS row_number
          FROM root_pages AS p
          JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_backdate DESC) a
WHERE a.row_number <= 2

result,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   1
43      def         {a}         2011-10-21...   2

Query,

SELECT *

FROM(
    SELECT 

        p.page_id AS page_id,
        p.page_url AS page_url,
        SUBSTRING(p.page_title,LOCATE('{',p.page_title),LOCATE('}',p.page_title)) AS group_title,
        p.page_created AS page_created,
         @url = p.page_url,
               @row_number := CASE 
                                WHEN p.page_url = @url THEN @row_number + 1 
                                ELSE 1
                              END AS row_number

    FROM root_pages AS p
    JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable

    WHERE p.parent_id = '8'
    AND p.page_id != '8'
    AND p.page_hide != '1'
    AND p.category_id = '1'

    ORDER BY p.page_backdate DESC


) a 
where  row_number <= 2

result,

page_id page_url    group_title page_created    row_number
----------------------------------------------------------
44      abc         {a}         2011-10-21...   1
43      def         {a}         2011-10-21...   1
42      qwe         {b}         2011-10-21...   1
41      rty         {b}         2011-10-21...   1
40      tyu         {c}         2011-10-21...   1
39      ghj         {c}         2011-10-21...   1
59      sss         {c}         2011-10-21...   1

EDIT 2:

Query,

SELECT 
    a.*,
    @r := CASE WHEN @g = a.group_title THEN @r+1 ELSE 1 END AS r,
    @g := a.group_title AS dummy

FROM(
    SELECT 

        p.page_id AS page_id,
        p.page_url AS page_url,
        SUBSTRING(p.page_title,LOCATE('{',p.page_title),LOCATE('}',p.page_title)) AS group_title,
        p.page_created AS page_created

    FROM root_pages AS p

    WHERE p.parent_id = '8'
    AND p.page_id != '8'
    AND p.page_hide != '1'
    AND p.category_id = '1'

    ORDER BY p.page_backdate DESC

) a

JOIN (SELECT @r := 0, @g) r -- Initialize the variable

Result,

page_id page_url    group_title page_created    dummy       row_number
44      abc         {a}         2011-10-21...   {a}                 1
43      def         {a}         2011-10-21...   {a}                 1
42      qwe         {b}         2011-10-21...   {b}                 1
41      rty         {b}         2011-10-21...   {b}                 1
40      tyu         {c}         2011-10-21...   {c}                 1
39      ghj         {c}         2011-10-21...   {c}                 1
59      sss         {c}         2011-10-21...   {c}                 1

ANSWER:

SELECT *

FROM
(
    SELECT 
        a.*,
        @r := CASE WHEN @g = a.group_title THEN @r+1 ELSE 1 END AS r,
        @g := a.group_title AS dummy

    FROM(
        SELECT 

            p.page_id AS page_id,
            p.page_url AS page_url,
            SUBSTRING(p.page_title,LOCATE('{',p.page_title),LOCATE('}',p.page_title)) AS group_title,
            p.page_created AS page_created

        FROM root_pages AS p

        WHERE p.parent_id = '8'
        AND p.page_id != '8'
        AND p.page_hide != '1'
        AND p.category_id = '1'

        ORDER BY p.page_backdate DESC

    ) a

    JOIN (SELECT @g:=null, @r:= 0) n -- Initialize the variable
) b
WHERE r <= 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

污味仙女 2024-12-18 06:57:01

您的查询未初始化 @row_number 变量:

SELECT a.*
  FROM (SELECT p.page_id,
               p.page_url,
               SUBSTRING(p.page_title, LOCATE('{',p.page_title),LOCATE('}', p.page_title)) AS group_title,
               p.page_created,
               @row_number := @row_number + 1 AS row_number
          FROM root_pages AS p
          JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_backdate DESC) a
WHERE a.row_number <= 2

否则,您需要在发布的查询之前使用 SET 声明。

其次,您发布的内容不包括重置变量所需的 CASE 语句,因此每个 page_url

SELECT a.*
  FROM (SELECT p.page_id,
               p.page_url,
               SUBSTRING(p.page_title, LOCATE('{',p.page_title),LOCATE('}', p.page_title)) AS group_title,
               p.page_created,
               @url = p.page_url,
               @row_number := CASE 
                                WHEN p.page_url = @url THEN @row_number + 1 
                                ELSE 1
                              END AS row_number
          FROM root_pages AS p
          JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_url, p.page_backdate DESC) a
WHERE a.row_number <= 2

IME 都会得到 1+,该变量适用于 InnoDB 表,但与 MyISAM 不一致。

Your query didn't initialize the @row_number variable:

SELECT a.*
  FROM (SELECT p.page_id,
               p.page_url,
               SUBSTRING(p.page_title, LOCATE('{',p.page_title),LOCATE('}', p.page_title)) AS group_title,
               p.page_created,
               @row_number := @row_number + 1 AS row_number
          FROM root_pages AS p
          JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_backdate DESC) a
WHERE a.row_number <= 2

Otherwise, you need to use a SET declaration before the query you posted.

Secondly, what you posted does not include the CASE statement necessary to reset the variable so you get 1+ for each page_url

SELECT a.*
  FROM (SELECT p.page_id,
               p.page_url,
               SUBSTRING(p.page_title, LOCATE('{',p.page_title),LOCATE('}', p.page_title)) AS group_title,
               p.page_created,
               @url = p.page_url,
               @row_number := CASE 
                                WHEN p.page_url = @url THEN @row_number + 1 
                                ELSE 1
                              END AS row_number
          FROM root_pages AS p
          JOIN (SELECT @row_number := 0, @url) r -- Initialize the variable
         WHERE p.parent_id = '8'
           AND p.page_id != '8'
           AND p.page_hide != '1'
           AND p.category_id = '1'
      ORDER BY p.page_url, p.page_backdate DESC) a
WHERE a.row_number <= 2

IME, the variable works on InnoDB tables but wasn't consistent against MyISAM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文