In the previous article, we learned how to create a database and a database table, and how to add records to a database table.
How can we retrieve data from database tables?
1. Retrieve Information from a database table
In fact, we have used the SELECT statement to retrieve information from database tables.
The SELECT statement format is generally:
Select search keyword from the retrieved table where search condition (optional)
The previously used "*" indicates Selecting All columns.
Next we will continue to use the table mytable we created in the previous article:
2. query all data:
Mysql> select * From mytable;
+ ---------- + ------ + ------------ + ---------- +
| Name | sex | birth | birthaddr |
+ ---------- + ------ + ------------ + -------- +
| Abccs | f | 1977-07-07 | China |
| Mary | f | 1978-12-12 | USA |
| Tom | M | 1970-09-02 | USA |
+ ---------- + ------ + ------------ + ---------- +
3 row in SET (0.00 Sec)
3. Corrected error records:
If the birth date of Tom is incorrect, it should be, you can use the update statement to correct it:
Mysql> Update mytable set birth = "1973-09-02" where name = "Tom ";
Use the statement in step 2 to check whether it has been corrected.
4. Select a Specific Row
We modified the birth date of Tom above. We can select Tom to see if there have been any changes:
Mysql> select * From mytable where name = "Tom ";
+ -------- + ------ + ------------ +
| Name | sex | birth | birthaddr |
+ -------- + ------ + ------------ +
| Tom | M | 1973-09-02 | USA |
+ -------- + ------ + ------------ +
1 row in SET (0.06 Sec)
The above where parameter specifies the search condition. We can also use combination conditions for queries:
Mysql> select * From mytable where sex = "F" and birthaddr = "China ";
+ -------- + ------ + ------------ +
| Name | sex | birth | birthaddr |
+ -------- + ------ + ------------ +
| Abccs | f | 1977-07-07 | China |
+ -------- + ------ + ------------ +
1 row in SET (0.06 Sec)
5. Select a specific column
If you want to view the names of all users in the table, you can perform the following operations:
Mysql> select name from mytable;
+ ---------- +
| Name |
+ ---------- +
| Abccs |
| Mary |
| Tom |
+ ---------- +
3 row in SET (0.00 Sec)
If you want to list the names and Gender columns, you can use commas to separate the keyword name and birth:
Myaql> select name, birth from mytable;
6. Sort rows
We can sort the records in the table by birthday size:
Mysql> select name, birth from mytable order by birth;
+ ---------- + ------------ +
| Name | birth |
+ ---------- + ------------ +
| Tom | 1973-09-02 |
| Abccs | 1977-07-07 |
| Mary | 1978-12-12 |
+ ---------- + ------------ +
3 row in SET (0.00 Sec)
We can use DESC for reverse sorting:
Mysql> select name, birth from mytable order by birth DESC;
+ ---------- + ------------ +
| Name | birth |
+ ---------- + ------------ +
| Mary | 1978-12-12 |
| Abccs | 1977-07-07 |
| Tom | 1973-09-02 |
+ ---------- + ------------ +
3 row in SET (0.00 Sec)
7. Row count
The database often needs to collect some data, such as the number of employees in the table, we need to use the row counting function count ().
The count () function is used to count records with non-null results:
Mysql> select count (*) from mytable;
+ ---------- +
| Count (*) |
+ ---------- +
| 3 |
+ ---------- +
1 row in SET (0.06 Sec)
Number of male and female employees:
Mysql> select sex, count (*) from mytable group by sex;
+ ------ + ---------- +
| Sex | count (*) |
+ ------ + ---------- +
| F | 2 |
| M | 1 |
+ ------ + ---------- +
2 row in SET (0.00 Sec)
Note that we use group by to group sex.