Php 使用或条件进行全文搜索的Mysql查询
我有一个下拉列表,其中只包含两个选项Php 使用或条件进行全文搜索的Mysql查询,php,mysql,sql,match-against,Php,Mysql,Sql,Match Against,我有一个下拉列表,其中只包含两个选项和和或。选择这两个选项后,将根据所选内容运行查询。当选择和时,将对查询进行更改。我的问题是这样的 $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
和
和或
。选择这两个选项后,将根据所选内容运行查询。当选择和
时,将对查询进行更改。我的问题是这样的
$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";
请注意与部分的
匹配,这是我想要添加我的和
以及或
条件的地方。如果选择和
条件时,查询将使用和
逻辑匹配节点名称
,而选择或
条件时,查询将使用或
逻辑匹配节点名称
,我该怎么办。任何帮助都可以。谢谢。如果您想根据前端的选择(存储在$condition
)添加条件,请尝试以下代码:(未测试)
你读过吗?@BillKarwin,我可以发誓我没有读到你提供的链接。我试试这个,看看是否有效。“多谢了,伙计。”比尔卡温,我怎么能说服你给你小费呢?你能发布你的答案吗?请投@diEcho一票,感谢你花时间展示一个更完整的例子。重要的是要给你很好的帮助,我不会为了分数而伤心@BillKarwin还有一个简短的问题,我如何确定查询是否有效?我尝试回显用于和
和或
的查询,结果相同。我错过什么了吗?
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 ..." ;