【问题标题】:Mysql query for full text search with an OR condition使用 OR 条件进行全文搜索的 Mysql 查询
【发布时间】:2013-09-27 19:40:37
【问题描述】:

我有一个下拉列表,其中仅包含两个选项 AndOr。选择这两个选项时将运行查询,但取决于选择。 When And is selected, a change in the query will be made.我的查询看起来像这样,

$sql = 
            "SELECT distinct n.node_id, n.path, 
               n.title_tag as node_name, 
               n2.title_tag as tree_name,
               MATCH (n.title_tag) AGAINST ('%s') as score
            FROM nodes n 
               join trees t on (n.tree_id=t.tree_id and t.status='A' and t.workspace_id=%d)
               join nodes n2 on (n2.node_id = t.tree_id)
               left join node_links nl1 on (n.node_id=nl1.from_node_id and nl1.to_node_id='%s')
               left join node_links nl2 on (n.node_id=nl2.to_node_id and nl2.from_node_id='%s')
            WHERE n.node_id!='%s' and nl1.node_link_id is null and nl2.node_link_id is null
            HAVING score > 0
            ORDER BY score DESC";

注意MATCH AGAINST 部分,这是我想添加AndOr 条件的地方。如果在选择And 条件时,查询将使用AND 逻辑匹配node_name,并且在选择Or 条件时,查询将使用OR 逻辑匹配node_name,我该怎么做。任何帮助都可以。谢谢。

【问题讨论】:

  • @BillKarwin,老实说,我可以发誓我错过了阅读您提供的链接。我会试试这个,看看这是否有效。非常感谢。
  • @BillKarwin,我将如何投票支持你给你的小费?你能发布你的答案吗?
  • 给@diEcho 点赞,花时间展示一个更完整的例子。重要的是给你很好的帮助,我不为积分而受伤! :-)
  • @BillKarwin 另一个快速问题,我将如何确定查询是否有效?我试图回显用于AndOr 的查询,结果相同。我错过了什么吗?

标签: php mysql sql match-against


【解决方案1】:

AFAIU 您想根据从前端选择的选项(存储在 $condition 中)添加条件的问题,如果是,请尝试以下代码:(未测试)

switch($condition) {
                case "AND" : $searchNode = "+$searchTerm"; break;
                case "OR"  : 
                default    :
                             $searchNode = "$searchTerm"; break;
}

$final_search = "$searchTitle $searchNode";

$sql = "SELECT distinct n.node_id, n.path, 
                   n.title_tag as node_name, 
                   n2.title_tag as tree_name,
                   MATCH (n.title_tag,n.node_name) 
                   AGAINST ($final_search IN BOOLEAN MODE) as score ..." ;

Reference

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多