我有一个下拉列表,其中只包含两个选项And
和Or
。但是,选择这两个选项后将运行查询,具体取决于所选内容。选择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
部分,这是我想添加And
和Or
条件的地方。如果在选择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