简体   繁体   中英

Multiple field query search without full text search

Query looks something like this:

$query = "
SELECT $field1, $field2, $field3, $field4, $field5 
FROM . $usertable 
WHERE $field1 = '$a' 
  AND $field2 = '$b'
  AND $field3 = '$c'
  AND $field4 = '$d'
  AND $field5 = '$e'
";

Now

  • a is a varchar(20)
  • b is a double
  • c is an int
  • d is an int
  • e is a varchar(25)
  • a - e ARE ALL USER INPUTS

Full text search is not an option , as a search must always return a result even if not an exact match.

Use LIKE :

$query = "  
SELECT $field1, $field2, $field3, $field4, $field5  
FROM $usertable  
WHERE $field1 LIKE '%$a%'  
  AND $field2 LIKE '%$b%'  
  AND $field3 LIKE '%$c%'  
  AND $field4 LIKE '%$d%'  
  AND field5 LIKE '%$e%'  
";  

The above solution only gives you result if all of search criteria are matched.
If you are looking for at least one is matched, use OR over AND .

$query = "  
SELECT $field1, $field2, $field3, $field4, $field5  
FROM $usertable  
WHERE $field1 LIKE '%$a%'  
  OR $field2 LIKE '%$b%'  
  OR $field3 LIKE '%$c%'  
  OR $field4 LIKE '%$d%'  
  OR field5 LIKE '%$e%'  
";  

In both of the cases above, query may not return a result if none of the search criteria is matched.
Result would be an empty set.

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