Situation in the table at the moment:
mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name | Lastname | Telephone |
+----+-----------+----------+-----------+
| 1 | John | Smith | 111333 |
| 2 | Anastasia | TheGreat | 555777 |
| 3 | Samantha | Fox | 456456 |
| 10 | Larry | TheFirst | 555888 |
| 11 | Bill | Gates | 444888 |
+----+-----------+----------+-----------+
5 rows in set (0.00 sec)
mysql>
These are two sample MySQL queries that insert data into the "friends" table:
mysql> INSERT INTO friends (id, Name, Lastname, Telephone)
-> VALUES (NULL, 'Bill', 'Gates', 444888);
Query OK, 1 row affected (0.16 sec)
mysql> INSERT INTO friends (id, Name, Lastname, Telephone)
-> VALUES (NULL, 'John', 'Smith', 111333);
Query OK, 1 row affected (0.11 sec)
mysql>
The first query inserts a new record with four values into the "friends" table.
The "INSERT INTO" statement is used to add data to a table in a MySQL database. The column names are listed in parentheses after the table name, followed by the keyword "VALUES", and then the values to be inserted in parentheses. In this case, a new record is being added to the table with "NULL" as the value for the "id" column (which should auto-increment).
The second query also inserts a new record into the "friends" table using the same syntax as the first query, but with different values.
Select report:
mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name | Lastname | Telephone |
+----+-----------+----------+-----------+
| 1 | John | Smith | 111333 |
| 2 | Anastasia | TheGreat | 555777 |
| 3 | Samantha | Fox | 456456 |
| 10 | Larry | TheFirst | 555888 |
| 11 | Bill | Gates | 444888 |
| 12 | Bill | Gates | 444888 |
| 13 | John | Smith | 111333 |
+----+-----------+----------+-----------+
7 rows in set (0.00 sec)
mysql>
Usage of "SELECT DISTINCT", targeting specific columns:
mysql> SELECT DISTINCT Name, Lastname, Telephone FROM friends;
+-----------+----------+-----------+
| Name | Lastname | Telephone |
+-----------+----------+-----------+
| John | Smith | 111333 |
| Anastasia | TheGreat | 555777 |
| Samantha | Fox | 456456 |
| Larry | TheFirst | 555888 |
| Bill | Gates | 444888 |
+-----------+----------+-----------+
5 rows in set (0.00 sec)
mysql>
The DISTINCT keyword is used to remove duplicate values from a result set. It is often used with the SELECT statement to select unique or distinct values from one or more columns of a table.
When the DISTINCT keyword is included in a SELECT statement, MySQL evaluates the values in the specified columns and returns only one instance of each unique value. In other words, if there are multiple rows in a table with the same value in the column(s) specified in the SELECT statement, only one of those rows will be included in the result set.
For example, the query "SELECT DISTINCT column1, column2 FROM table" will return a result set with unique combinations of values from the "column1" and "column2" columns of the "table". The DISTINCT keyword can be used with one or more columns, depending on the specific use case.
It's important to note that the DISTINCT keyword considers the entire value in a column when determining uniqueness. So, for example, if a column contains values with leading or trailing whitespace, those values will be considered distinct even if they appear identical when displayed.
The DISTINCT keyword is a useful tool for filtering and analyzing data in a MySQL database, and can be used to remove redundancy from result sets to help streamline analysis and reporting.
No comments:
Post a Comment