Search code examples
phpmysqlsql-search

PHP searching from fixed rows


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.)


Solution

  • 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