在页面分割输出中合并来自不同表的搜索结果

发布于 2024-10-04 01:16:28 字数 338 浏览 2 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(1

A君 2024-10-11 01:16:28

这是执行此操作的基本方法。对于此示例,每个表都有一个“id”和一个“timestamp”字段,我们希望按时间戳字段对结果进行排序。

<?
function bubblesort(array $items) {
    // Sorts the items in a two-dimensional array based on a
    // timestamp in the first field of the arrays passed.
    do{
        $flag = false;
        for ($i = 0; $i < (count($items) - 1); $i++) {
            if(strtotime($items[$i][0]) > strtotime($items[$i + 1][0])) {
                $swap = $items[$i];
                $items[$i] = $items[$i + 1];
                $items[$i + 1] = $swap;

                $flag = true;
            }
        }
    }
    while($flag);
    return $items;
}
$results = array();

// Get results for the first table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table1`;");
if (!$result)
    die(mysql_error());
while ($row = mysql_fetch_array($result))
    $results[] = array($row[0], $row[1]);

// Get results for the second table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table2`;");
if (!$result)
    die(mysql_error());
while ($row = mysql_fetch_array($result))
    $results[] = array($row[0], $row[1]);

// Get results for the third table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table3`;");
if (!$result)
    die(mysql_error());
while ($row = mysql_fetch_array($result))
    $results[] = array($row[0], $row[1]);

// Sort the joint results
$results = bubblesort($results);
?>

当然,每个查询都必须针对单个表进行调整。请注意,我使用了冒泡排序算法,根据您期望的结果数量,不同的排序算法可能会更好。

使用 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.

<?
function bubblesort(array $items) {
    // Sorts the items in a two-dimensional array based on a
    // timestamp in the first field of the arrays passed.
    do{
        $flag = false;
        for ($i = 0; $i < (count($items) - 1); $i++) {
            if(strtotime($items[$i][0]) > strtotime($items[$i + 1][0])) {
                $swap = $items[$i];
                $items[$i] = $items[$i + 1];
                $items[$i + 1] = $swap;

                $flag = true;
            }
        }
    }
    while($flag);
    return $items;
}
$results = array();

// Get results for the first table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table1`;");
if (!$result)
    die(mysql_error());
while ($row = mysql_fetch_array($result))
    $results[] = array($row[0], $row[1]);

// Get results for the second table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table2`;");
if (!$result)
    die(mysql_error());
while ($row = mysql_fetch_array($result))
    $results[] = array($row[0], $row[1]);

// Get results for the third table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table3`;");
if (!$result)
    die(mysql_error());
while ($row = mysql_fetch_array($result))
    $results[] = array($row[0], $row[1]);

// Sort the joint results
$results = bubblesort($results);
?>

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.

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