[英]Mysql query for full text search with an OR condition
我有一個下拉列表,其中只包含兩個選項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 ..." ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.