php中的多选查询

时间:2017-08-05 07:20:31

标签: php sql

如何从我的表中选择一个列表来搜索2行,例如此查询:

SELECT * FROM customer WHERE first_name and last_name LIKE '%$input%';

这不起作用。我想搜索表中的2列...
有什么想法吗? 看看这个例子

user type in input : amin heydari and the first name in table is amin and 
last name is heydari. if i add 'and' in query: if user type 'a' page show 
list --> a in amin and a in heydari i want to show list if type amin 
heydari...

1 个答案:

答案 0 :(得分:0)

你可以这样做: -

$input = explode(' ',trim($input));

if(count($input)==2){
   SELECT * FROM customer WHERE first_name LIKE '%$input[0]%' OR last_name LIKE '%$input[1]%';
}
if(count($input)==1){
   SELECT * FROM customer WHERE first_name LIKE '%$input[0]%' OR last_name LIKE '%$input[0]%';
}
if(count($input)>2){
   SELECT * FROM customer WHERE first_name LIKE '%$input[0]%' OR last_name LIKE '%$input[1]%';
}

查询将如下所示: - https://eval.in/841836