0

我有一个下拉列表,其中仅包含两个选项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如果在选择条件时查询将与node_name使用AND逻辑匹配,并且在选择Or条件时查询将与node_name使用逻辑匹配,我该怎么做OR。任何帮助都可以。谢谢。

4

1 回答 1

2

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

于 2013-09-27T19:51:46.380 回答