Computer >> Computer tutorials >  >> Programming >> MySQL

Does NOT EQUAL exist in MySQL?


Yes, NOT EQUAL exist in MySQL in the fome of <> operator. The syntax is as follows−

SELECT * FROM yourTableName WHERE yourColumnName <> yourValue;

To understand the above syntax, let us create a table. The query to create a table is as follows−

mysql> create table DoesNotEqualDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.98 sec)

Insert some records in the table using insert command. The query to insert record is as follows−

mysql> insert into DoesNotEqualDemo(Name) values(NULL);
Query OK, 1 row affected (0.24 sec)

mysql> insert into DoesNotEqualDemo(Name) values('John');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DoesNotEqualDemo(Name) values('Carol');
Query OK, 1 row affected (0.43 sec)

mysql> insert into DoesNotEqualDemo(Name) values('Bob');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DoesNotEqualDemo(Name) values('');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DoesNotEqualDemo(Name) values('Larry');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DoesNotEqualDemo(Name) values(NULL);
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement. The query is as follows−

mysql> select *from DoesNotEqualDemo;

The following is the output−

+----+-------+
| Id | Name  |
+----+-------+
|  1 | NULL  |
|  2 | John  |
|  3 | Carol |
|  4 | Bob   |
|  5 |       |
|  6 | Larry |
|  7 | NULL  |
+----+-------+
7 rows in set (0.00 sec)

Here is the query to select all records which are not equal to NULL as well as empty string−

mysql> select *from DoesNotEqualDemo where Name <> 'NULL' and Name <> '';

The following is the output−

+----+-------+
| Id | Name  |
+----+-------+
|  2 | John  |
|  3 | Carol |
|  4 | Bob   |
|  6 | Larry |
+----+-------+
4 rows in set (0.00 sec)