Group pages from one column

陌路散爱 提交于 2020-01-17 01:13:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7870221/group-pages-from-one-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!