Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable723 (Value int);
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable723 values(100);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable723 values(200);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable723 values(200);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable723 values(100);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable723 values(300);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable723 values(400);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable723 values(400);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable723 values(600);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable723 values(400);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable723 values(200);
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable723;

This will produce the following output -

+-------+
| Value |
+-------+
| 100   |
| 200   |
| 200   |
| 100   |
| 300   |
| 400   |
| 400   |
| 600   |
| 400   |
| 200   |
+-------+
10 rows in set (0.00 sec)

Following is the query to select a value from MySQL database only if it exists once from a column with duplicate and non-duplicate values −

mysql> select *from DemoTable723 group by Value having count(Value)=1;

This will produce the following output -

+-------+
| Value |
+-------+
| 300   |
| 600   |
+-------+
2 rows in set (0.00 sec)
raja
Published on 22-Aug-2019 10:57:37
Advertisements