1

I have patient table with 1000+ patient records. All records are created by 5 admins. I have separated patients in patient table using admin_id column. When admin "A" login then he only can see those patient that created by him. Now i wanna search patient from patient table. My search result will be only form patient records those are created by admin "A";

MySQL query:

SELECT 
    *
FROM
    patient
WHERE
    admin_id = 36 AND fname LIKE '%test%'
        OR email LIKE '%test%'
        OR mobile LIKE '%test%'
ORDER BY ID DESC 

(Here I get result from all records but I need only from where admin_id = 36.)

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Fahim Amin
  • 17
  • 4

2 Answers2

4

You have to put around the OR close a parentheses () so that the expression only is true when admin_id is 36 and the rest also is true

SELECT 
    *
FROM
    patient
WHERE
    admin_id = 36 AND (fname LIKE '%test%'
        OR email LIKE '%test%'
        OR mobile LIKE '%test%')
ORDER BY ID DESC 
tadman
  • 208,517
  • 23
  • 234
  • 262
nbk
  • 45,398
  • 8
  • 30
  • 47
0

Try:-

SELECT 
    *
FROM
    patient
WHERE
    (admin_id = 36 AND fname LIKE '%test%')
        OR (email LIKE '%test%')
        OR (mobile LIKE '%test%')
ORDER BY ID DESC 
Chris
  • 987
  • 2
  • 12
  • 25