将一列中的页面分组

发布于 2024-12-11 09:12:22 字数 1896 浏览 0 评论 0原文

我的数据库中的页表中有这些信息,

page_id     page_title

1           xy {a}
2           kp {a}
3           xyz {b}
4           mno {b}
5           abc {c}
6           mno {c}
7           qwe {c}

我使用大括号对页面进行分组,例如页面xykp属于a组。

所以我想返回这个信息中的结果,

group_title     page_title
a               xy
a               kp
b               xyz
b               mno
c               abc
c               mno

但是每组最多两页

我可以使用 SQL 查询或 PHP 来实现此目的吗?

这就是我正在使用 REGEXP 进行的工作...

SELECT 
p.page_title REGEXP '{[a-z]}' AS groupTitle

FROM pages AS p

ORDER BY p.page_created DESC

我得到这个结果,

groupTitle
0
0
0
0
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

但我仍然有限制每个组的行数的问题,有什么想法吗?

I have these information in a page table in my db,

page_id     page_title

1           xy {a}
2           kp {a}
3           xyz {b}
4           mno {b}
5           abc {c}
6           mno {c}
7           qwe {c}

I use curly brackets to group pages, such as page xy and kp belong to the group of a.

So I want to return the result in this information,

group_title     page_title
a               xy
a               kp
b               xyz
b               mno
c               abc
c               mno

But maximum of two pages in each group.

Can I use SQL query to achieve this or PHP?

This is what I am working on by using REGEXP...

SELECT 
p.page_title REGEXP '{[a-z]}' AS groupTitle

FROM pages AS p

ORDER BY p.page_created DESC

I get this result,

groupTitle
0
0
0
0
0
0

Any ideas?

EDIT:

Here is my 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,
        @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 still have the problem to limit the number of row from each group, any ideas?

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

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

发布评论

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

评论(1

一片旧的回忆 2024-12-18 09:12:22

将名为 group_id (或类似)的列添加到页表中。

如果想要选择一个组,请使用 WHERE group_id = 从数据库中选择,或者使用 ORDER BY group_id 按组排序。

编辑

如果您必须坚持这个概念,您可以将 page_title 拆分为相关组件。我不会向您展示有效的查询和排序例程,而是帮助您开始:

$page_title_separated = preg_split("/[\{\}]+/", $page_title);

其中 $page_title 是数据库中的页面标题。

按照您的示例(第一行条目),这将导致

$page_title_separated[0] => xy
$page_title_separated[1] => a

良好的结果,并且为了完整起见,请提及将有一个 $page_title_separated[2],它将是空的。此外,真正的标题,即 $page_title_separated[0] 将以空格结尾。

我假设您知道如何在 php 中对数组进行排序以及稍后如何处理这些数据。

有很多更巧妙的方法可以做到这一点,但是 - 我怎么强调都不为过 - 最巧妙的仍然是第三列。

Add a column called group_id (or the like) to your page table.

Select from the db with WHERE group_id = if wanting to select one group or ORDER BY group_id to, well, order by group.

EDIT

If you must stick to this concept, you could split the page_title into relevant components. I'm not going present you a working query and sorting routine, but to get you started:

$page_title_separated = preg_split("/[\{\}]+/", $page_title);

where $page_title is a page title from your db.

Following your example (1st row entry), this would result in

$page_title_separated[0] => xy
$page_title_separated[1] => a

well, and for the sake of completeness, let it be mentioned that there'd be a $page_title_separated[2], which would be empty. Also, the real title, i.e. $page_title_separated[0] would end in a whitespace.

I assume you know how to sort arrays in php and what to do with this data later on.

There's a multitude of slicker ways of doing this, but - I can't stress this enough - the slickest remains a third column.

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