在页面分割输出中合并来自不同表的搜索结果
我有 3 个实体,每个实体都有一个对应的表。我的任务是在这 3 个表中进行搜索,因此结果将显示在一页上,并根据排序条件混合在一起。我正在寻找合适的解决方案。我有一些想法。
可以对每个表执行 3 次搜索查询,然后考虑排序条件显示结果。当我将搜索结果全部放在一页上时,这很简单。当我需要将结果拆分为页面时,我可以使用以下算法:我运行相同的 3 个搜索查询,并以必要的排序顺序将结果行 ID 和表名称保存在会话中以供当前搜索。我只在搜索开始时执行这些查询一次。 根据所选页面,我将在会话中查找 ID,并执行 3 个简单查询以按 ID 查找行。
您有什么想法如何更好地解决这个问题吗? 我是在MySQL+PHP+Zend框架基础上做的。
I have 3 entities and each has a corresponding table. I have a task to search in these 3 tables so results will be shown at one page, mixing together according to sort conditions. I am looking for a proper solution. I have some ideas.
It's possible to perform 3 search queries for each table and then show the results in consideration of sort conditions. It is simple when I have the search results all on one page. When I need to split results to pages, I can use the following algorithm: I run the same 3 search queries and I save result rows IDs and table names in session for current search in the necessary sort order. I perform these queries only once at search start.
According to the selected page I will find IDs in session and perform 3 simple queries to find rows by their IDs.
Do you have any ideas how to solve this problem better?
I do it on the MySQL+PHP+Zend framework base.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是执行此操作的基本方法。对于此示例,每个表都有一个“id”和一个“timestamp”字段,我们希望按时间戳字段对结果进行排序。
当然,每个查询都必须针对单个表进行调整。请注意,我使用了冒泡排序算法,根据您期望的结果数量,不同的排序算法可能会更好。
使用 UNION 将三个 SELECT 查询合并为一个可能会更好(请参阅 文档),但我对表的结构了解不够,无法告诉您所需的查询。
Here's a basic way of doing this. For this example, each table has an 'id' and a 'timestamp' field, and we want to sort the results by the timestamp field.
Of course, each of the queries will have to be adapted for the individual table. Note that I've used a bubble sort alogrithm, depending on the amount of results you're expecting, a different sorting algorithm might be better.
It may also be better to merge the three SELECT queries into one by using UNION (see the documentation), but I don't know enough about the structure of the tables to tell you the query you'd need.