Search code examples
mysqlsqlsql-like

LIKE... AND NOT... sql


I am creating a query in which i am selecting data from multiple tables, however, i do not want to select the entry that my users are currently viewing. I have completed this in other query using the AND NOT syntax but when using this in conjunction with LIKE i still return the row i am NOT looking for?

The syntax has no errors when i run but still is incorrect. Here is my query.

SELECT id AS num, mix_title AS title, table_type AS type, header, post_date, counter 
FROM mixes
WHERE keywords LIKE ('%#workout%')
   OR keywords LIKE ('%#drunk_fairy%')
 AND NOT id='1'

I would like the above code to return all LIKE rows but NOT where the id is equal to 1.

Apologies if this has already been asked, i wasn't too sure how to ask the question, Thanks.


Solution

  • You have to use parentheses because AND takes precedence over OR:

    SELECT id AS num, mix_title AS title, table_type AS type, 
           header, post_date, counter 
    FROM mixes 
    WHERE (keywords LIKE ('%#workout%') OR keywords LIKE('%#drunk_fairy%'))
          AND NOT id='1'