Wednesday, May 8, 2013

Exclude a certain row from an MYSQL query

You can exclude a certain row from sql query using '<>' or '!=' clause.

For example,  if you want to exclude the row in the result with user_id => 1 from the table 'poll', following query will give you the desired result.

SELECT *  FROM `poll` WHERE  user_id <> 1

If the data type of id is String you must use 
SELECT *  FROM `poll ` WHERE  user_id <> '1'

Note: In MySQL, you can also use != rather than <> but <> is ANSI and more portable.

1 comment:

  1. To exclude more than 1 row, is better to use NOT IN (col1, col2...) statement. Anyway, it was really helpful. :)