如何在此查询语句中添加LIKE查询而不是相等?

时间:2017-06-18 18:12:53

标签: mysql

我在查询中添加LIKE时出现此错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' OR agent_ip = 'Counter' OR port = 'Counter' OR 0home_ip_por' at line 45

我的代码是:

'.($search_field ?" AND home_id = '$search_field' OR user_id_main = '$search_field' OR home_path = '$search_field' 
                            OR home_name = '$search_field' 
                            OR user_id_main IN (SELECT `user_id` FROM `".$this->table_prefix."users` WHERE users_login = '$search_field')
                            OR user_id = '$search_field'
                            OR user_id IN (SELECT `user_id` FROM `".$this->table_prefix."users` WHERE users_login = '$search_field')
                            OR agent_ip = '$search_field' OR port = '$search_field'
                            " : '').

我需要将=替换为LIKE中的OR home_name = '$search_field'

1 个答案:

答案 0 :(得分:0)

试试这个:

'.($search_field ?" AND home_id = '$search_field' OR user_id_main = '$search_field' OR home_path = '$search_field' OR home_name LIKE '%".$search_field."%' OR user_id_main IN (SELECT `user_id` FROM `".$this->table_prefix."users` WHERE users_login = '$search_field') OR user_id = '$search_field' OR user_id IN (SELECT `user_id` FROM `".$this->table_prefix."users` WHERE users_login = '$search_field') OR agent_ip = '$search_field' OR port = '$search_field'