Tirth Patel
Tirth Patel

Reputation: 5736

MySQL multiple LIKE condition not working

I have following 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%'

My query result satisfies only first LIKE condition (seller.name LIKE '%$search%'), and doesn't satisfies second LIKE condition (login.email LIKE '%$search%')

For Example If $search = "j", then it returns only one row that is,

seller_id | name  | email        
--------------------------------
 1        | jay   | [email protected]

But I have second condition too that is login.email LIKE '%$search%' So for this example my desired result would be:

seller_id | name  | email        
---------------------------------
 1        | jay   | [email protected]
 4        | vsm   | [email protected]

I have following Tables.

Table: Login

login_id | email             | password
---------------------------------------
 4       | [email protected]      | 123
 5       | [email protected]  | 123
 6       | [email protected]     | 123
 7       | [email protected]     | 123

Table: Seller

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

Upvotes: 0

Views: 75

Answers (1)

jmoerdyk
jmoerdyk

Reputation: 5518

On the line

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

AND will only return results that match both conditions at the same time, you want an OR instead:

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%')

Upvotes: 1

Related Questions