将一列中的页面分组
我的数据库中的页表
中有这些信息,
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}
我使用大括号对页面进行分组,例如页面xy
和kp
属于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将名为 group_id (或类似)的列添加到页表中。
如果想要选择一个组,请使用
WHERE group_id =
从数据库中选择,或者使用ORDER BY group_id
按组排序。编辑
如果您必须坚持这个概念,您可以将 page_title 拆分为相关组件。我不会向您展示有效的查询和排序例程,而是帮助您开始:
其中
$page_title
是数据库中的页面标题。按照您的示例(第一行条目),这将导致
良好的结果,并且为了完整起见,请提及将有一个
$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 orORDER 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:
where
$page_title
is a page title from your db.Following your example (1st row entry), this would result in
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.