How to retrieve data from a MySQL database table _ MySQL
Source: Internet
Author: User
1. retrieving information from the database table. In fact, we have used the SELECT statement to retrieve information from the database table. The select statement format is generally: SELECT the query keyword FROM the table to be searched WHERE the query condition (optional) previously used "*" indicates that all columns are selected. Next we will continue to use the table mytabl created in the previous article.
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 mytable table 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)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.