[MySQL Basic Learning] Show Unique Value from a Table

in #mysql7 years ago

Hi again!

With a glass of Kopi Lanang - Dampit (http://www.malangtimes.com/baca/13954/20160820/132007/kopi-dampit-malang-salah-satu-kopi-terbaik-dunia/) to cure my longing for the atmosphere of coffee time in Aceh, and with great enthusiasm to finish writing my thesis (desperately need to be finished within two weeks :(), I've found an old note about working with MySQL, accidentally. I think it worth to be shared here. Here we go!

photo_2018-01-31_15-16-55.jpg

This time, we want to show unique value from a MySQL table with these values:

sql.PNG

Let's solve this problem by creating a MySQL database and named it as "unique", and create a table "test" with above data and structure.

This is how I've done it using Linux terminal:


[root@adisun ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3641
Server version: 5.1.52 Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| unique |
+--------------------+
3 rows in set (0.00 sec)

mysql> use unique;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_unique |
+-----------------+
| test |
+-----------------+
1 row in set (0.00 sec)

mysql> select * from test;
+-----+-------+----------+
| ID | Nama | Alamat |
+-----+-------+----------+
| 11 | adi | medan |
| 12 | jack | banda aceh |
| 44 | penan | sigli |
| 11 | adi | medan |
| 87 | albert | bireuen |
| 44 | penan | sigli |
| 11 | adi | medan |
| 12 | jack | banda aceh |
| 13 | adi | medan |
+-----+-------+----------+
9 rows in set (0.00 sec)

mysql> select distinct Nama from test;
+-------+
| Nama |
+-------+
| adi |
| jack |
| penan |
| albert |
+-------+
4 rows in set (0.00 sec)

mysql> select distinct ID,Nama,Alamat from test;
+-----+-------+----------+
| ID | Nama | Alamat |
+-----+-------+----------+
| 11 | adi | medan |
| 12 | jack | banda aceh |
| 44 | penan | sigli |
| 87 | albert | bireuen |
| 13 | adi | medan |
+-----+-------+----------+
5 rows in set (0.00 sec)


Can you see the different between each query? I'm sure you can!

Hopefully this post will be useful :)