简体   繁体   English

MYSQL与PHP中的查询匹配

[英]MYSQL match against query in PHP

I want to use the MATCH AGAINST query instead of like. 我想使用MATCH AGAINST查询而不是喜欢。 I want to search in my database for a certain value whose attribute is not specified (like WHERE id = 2045). 我想在数据库中搜索未指定属性的某个值(例如WHERE id = 2045)。 I have a search bar like google in my site and I want to write for example: 2045 or John and this gives me all about it. 我的网站上有一个类似google的搜索栏,例如,我想写:2045或John,这给了我一切。 I dont want to search only for years ou names. 我只想搜索年份或名称。 I already have this for a specific attribute as name: 我已经将其作为特定属性的名称:

$name = $_POST["name"];

$sql="SELECT id, name, city FROM student WHERE name LIKE '".$name."%'";

But what I want is something with the MATCH AGAINST query. 但是我想要的是MATCH AGAINST查询。 I don't know how to do that... This is what I have: 我不知道该怎么做...这就是我所拥有的:

$sql ="SELECT id, name, city FROM student WHERE MATCH(id, name, city) AGAINST ('".$name."%')";

Here, I want my $name to be something random that I wrote in my search bar. 在这里,我希望我的$ name是我在搜索栏中写的随机名称。 Thank you 谢谢

Do you've any specific reason not using multipe LIKE with OR ? 您是否有任何特定原因不使用OR multipe LIKE

$name = $_POST["name"];
$sql="SELECT id, name, city FROM student 
     WHERE id LIKE '".$name."%'" OR
           name LIKE '".$name."%'" OR
           city LIKE '".$name."%'";

Another way to do it using CONCAT() , 使用CONCAT()另一种方法,

$sql="SELECT id, name, city FROM student WHERE CONCAT(id,name,city) LIKE '".$name."%'"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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