简体   繁体   中英

Mysql query for full text search with an OR condition

I have a dropdown list where it contains only two options And and Or . These two options when selected will run a query however depending on the selection. When And is selected, a change in the query will be made. My query looks like this,

$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";

Notice the MATCH AGAINST part, that is where I would like to add my And and Or conditions. How would I do it if when selecting the And condition the query will match the node_name using the AND logic and when selecting the Or condition the query will match the node_name using the OR logic. Any help will do. Thanks.

AFAIU the question you want to add a condition based on the selection of a choice (stored in $condition ) from front end, if so then please give a try to below code: (not tested)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM