Home  >  Article  >  Database  >  Does NOT EQUAL exist in MySQL?

Does NOT EQUAL exist in MySQL?

WBOY
WBOYforward
2023-09-05 20:41:071248browse

MySQL 中不存在 NOT EQUAL 吗?

Yes, NOT EQUAL exists in MySQL in the form of the 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 the 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)

Use the insert command to insert some records in the table. The query to insert records is as follows -

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

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

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

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

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

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

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

Use the select statement to display all records in the table. 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 that are not equal to NULL and empty string-

mysql> select *from DoesNotEqualDemo where Name <> &#39;NULL&#39; and Name <> &#39;&#39;;

The following is the output-

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

The above is the detailed content of Does NOT EQUAL exist in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete