MySQL: Not Operator

Profile picture for user arilio666

The NOT operator from the name does the process of selecting table contents based on the condition provided where it selects everything but except the content provided inside the NOT condition.

To put it simply it selects everything except the condition provided content from the table.

Syntax

SELECT * from TableName WHERE NOT (condition1 (comparison) value)

From the syntax, the condition value is not selected except that it displays everything.

Let us see it in action with our real-time example with the customer's table.

So here is the customer's table.

We will be doing the following things to this table:

  1. Select the respective table column names customer_id, first_name, last_name, and state.
  2. Display customers from states except 'VA' and 'MA'

Example

select customer_id, first_name, last_name, state from customers where  NOT (state = 'VA' or state = 'MA')

  • So from here we selected the customer id, first_name, last_name, state from the table customers and display the list of all except the states of VA and MA states respectively.
  • So this is a way you can neglect or negate a condition based on the respective need.
Tags