Mysql 查询,用于使用 OR 条件进行全文搜索



我有一个下拉列表,其中只包含两个选项AndOr。但是,选择这两个选项后将运行查询,具体取决于所选内容。选择And后,将对查询进行更改。我的查询如下所示,

$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,我将如何做到这一点。任何帮助都可以。谢谢。

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

最新更新