php中多对多字段的标签搜索

发布于 2024-08-05 18:18:58 字数 451 浏览 7 评论 0原文

我正在尝试为简报系统做一个搜索引擎。

我正在使用多对多关系样式设置,其中 3 个数据库代表关系。

comms_briefings 是简报数据的存储位置 search_tags 是存储标签的位置 Briefings_tags 是建立关系的地方,

我已经有此代码,但是几乎已经完美,由于某种原因,如果一个标签在简报中出现多次,它会多次返回数据库中的同一行。

例如,如果用户搜索

$“复杂查询”

系统将返回任何具有标签 complx 的行,并且查询两次,因为有两个匹配的标签。我希望结果按降序排列,首先获得最多结果的行并且仅出现一次。

有什么想法吗? :)

i'm trying to do a search engine for a briefing system.

I'm using a many to many relationship style setup with 3 databases representing the relationships.

comms_briefings is where the data is stored for the briefings
search_tags is where the tags are stored
briefings_tags is where the relationship is formed

i have this code already which ALMOST perfect however, for some reason it returns the same row in the database multiple times if a tag appears more than once in the briefing.

eg if user searchs for

$ "complx enquiries"

the system will return any row that has the tag complx and enquiries twice as there are two matching tags. I want the results to be ordered desc with the row that gets the most results first and only to appear once.

any ideas? :)

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

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

发布评论

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

评论(1

桃气十足 2024-08-12 18:18:58

应该这样做:

function searchTags($tags) {

    $tags = explode(' ', $tags);
    $tagIds = array();

    foreach($tags as $key) {
        $sql = mysql_query("SELECT `id` FROM `search_tags` WHERE `tag` = '".$key."'");
        while($r=mysql_fetch_object($sql))
            $tagIds[] = $r->id;
    }

    $query = mysql_query("SELECT comms_briefings.*, COUNT(*) AS num
        FROM comms_briefings, briefings_tags
        WHERE briefings_tags.tag_id IN ('" . implode("','", $tagIds) ."') AND briefings_tags.briefing_id = comms_briefings.id
            GROUP BY comms_briefines.id
        ORDER BY num DESC, `created` DESC"); 

    while($r = mysql_fetch_object($query)) {

        $summation = preg_replace('^(.*)\n(.*)^', '$1\\n$2', $r->summation);
        $summation = preg_replace('/[\r]+/', '', $summation);

        echo '<li class="message unread" id="msg-'.$r->id.'">';
        echo '<h3><a href="">'.$r->title.'</a></h3>';
        echo '<ul class="details">
                <li><span>Importance:</span> '.$r->importance.'</li>
                <li><span>Date:</span> '.$r->created.'</li>
                <li><span>Summary:</span> '.$summation.'</li>
             </ul>
             <div class="clr"></div>
             </li>';
    }
}

首先将所有 tagId 拉入一个数组,然后对简报执行单个查询,按 id 分组。

Something like this should do:

function searchTags($tags) {

    $tags = explode(' ', $tags);
    $tagIds = array();

    foreach($tags as $key) {
        $sql = mysql_query("SELECT `id` FROM `search_tags` WHERE `tag` = '".$key."'");
        while($r=mysql_fetch_object($sql))
            $tagIds[] = $r->id;
    }

    $query = mysql_query("SELECT comms_briefings.*, COUNT(*) AS num
        FROM comms_briefings, briefings_tags
        WHERE briefings_tags.tag_id IN ('" . implode("','", $tagIds) ."') AND briefings_tags.briefing_id = comms_briefings.id
            GROUP BY comms_briefines.id
        ORDER BY num DESC, `created` DESC"); 

    while($r = mysql_fetch_object($query)) {

        $summation = preg_replace('^(.*)\n(.*)^', '$1\\n$2', $r->summation);
        $summation = preg_replace('/[\r]+/', '', $summation);

        echo '<li class="message unread" id="msg-'.$r->id.'">';
        echo '<h3><a href="">'.$r->title.'</a></h3>';
        echo '<ul class="details">
                <li><span>Importance:</span> '.$r->importance.'</li>
                <li><span>Date:</span> '.$r->created.'</li>
                <li><span>Summary:</span> '.$summation.'</li>
             </ul>
             <div class="clr"></div>
             </li>';
    }
}

All the tagIds are pulled into an array first, then you do a single query on the briefings, grouping by id.

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