MySQL多个LIKE条件不起作用

时间:2016-04-06 19:07:15

标签: mysql

我关注query

SELECT seller_id, name, email, FROM login, seller
WHERE login.login_id = seller.login_id
AND
    seller.name LIKE '%$search%' AND login.email LIKE '%$search%'

我的查询结果仅满足第一个LIKE条件(seller.name LIKE '%$search%'),并且不满足第二个LIKE条件(login.email LIKE '%$search%'

例如 如果是$search = "j",那么它只返回一行

seller_id | name  | email        
--------------------------------
 1        | jay   | jd@gmail.com

但我也有第二个条件是login.email LIKE '%$search%'所以对于这个例子,我想要的结果是:

seller_id | name  | email        
---------------------------------
 1        | jay   | jd@gmail.com
 4        | vsm   | jay@gmail.com

我有表格

表:登录

login_id | email             | password
---------------------------------------
 4       | jd@gmail.com      | 123
 5       | fluttr@gmail.com  | 123
 6       | amy@gmail.com     | 123
 7       | jay@gmail.com     | 123

表:卖方

seller_id | login_id | name  
-----------------------------
 1        | 4        | jay
 2        | 5        | rajesh
 3        | 6        | ema
 4        | 7        | vsm

1 个答案:

答案 0 :(得分:1)

在线

seller.name LIKE '%$search%' AND login.email LIKE '%$search%'

AND只会同时返回与两个条件匹配的结果,而您需要OR代替:

SELECT seller_id, name, email, FROM login, seller
WHERE login.login_id = seller.login_id
AND 
     (seller.name LIKE '%$search%' OR login.email LIKE '%$search%')