It is not difficult to retrieve data from MySQL database tables.

Source: Internet
Author: User

Today, we will mainly talk about how to correctly retrieve related data from MySQL database tables, if you are interested in the actual operations on the MySQL database table to retrieve the relevant data, you can click to view the following article.

1. Retrieve Information from the MySQL database table

In fact, we have used the SELECT statement to retrieve information from the MySQL database table.

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:

 
 
  1. MySQL (the best combination with PHP)> select * from mytable;
  2. + ---------- + ------ + ------------ + ---------- +
  3. | Name | sex | birth | birthaddr |
  4. + ---------- + ------ + ------------ + -------- +
  5. | Abccs | f | 1977-07-07 | china |
  6. | Mary | f | 1978-12-12 | usa |
  7. | Tom | m | 1970-09-02 | usa |
  8. + ---------- + ------ + ------------ + ---------- +
  9. 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 (the best combination with PHP)> 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:

 
 
  1. MySQL (best combination with PHP)> select * from mytable where name = "tom ";
  2. + -------- + ------ + ------------ +
  3. | Name | sex | birth | birthaddr |
  4. + -------- + ------ + ------------ +
  5. | Tom | m | 1973-09-02 | usa |
  6. + -------- + ------ + ------------ +
  7. 1 row in set (0.06 sec)

The above WHERE parameter specifies the search condition. We can also use combination conditions for queries:

 
 
  1. MySQL (best combination with PHP)> SELECT * FROM mytable WHERE sex = "f" AND birthaddr = "china ";
  2. + -------- + ------ + ------------ +
  3. | Name | sex | birth | birthaddr |
  4. + -------- + ------ + ------------ +
  5. | Abccs | f | 1977-07-07 | china |
  6. + -------- + ------ + ------------ +
  7. 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:

 
 
  1. MySQL (best combination with PHP)> SELECT name FROM mytable;
  2. + ---------- +
  3. | Name |
  4. + ---------- +
  5. | Abccs |
  6. | Mary |
  7. | Tom |
  8. + ---------- +
  9. 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:

 
 
  1. MySQL (best combination with PHP)> SELECT name, birth FROM mytable order by birth;
  2. + ---------- + ------------ +
  3. | Name | birth |
  4. + ---------- + ------------ +
  5. | Tom | 1973-09-02 |
  6. | Abccs | 1977-07-07 |
  7. | Mary | 1978-12-12 |
  8. + ---------- + ------------ +
  9. 3 row in set (0.00 sec)

We can use DESC for reverse sorting:

 
 
  1. MySQL (best combination with PHP)> SELECT name, birth FROM mytable order by birth DESC;
  2. + ---------- + ------------ +
  3. | Name | birth |
  4. + ---------- + ------------ +
  5. | Mary | 1978-12-12 |
  6. | Abccs | 1977-07-07 |
  7. | Tom | 1973-09-02 |
  8. + ---------- + ------------ +
  9. 3 row in set (0.00 sec)

7. Row count

MySQL databases often need 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:

 
 
  1. MySQL (the best combination with PHP)> select count (*) FROM mytable;
  2. + ---------- +
  3. | COUNT (*) |
  4. + ---------- +
  5. | 3 |
  6. + ---------- +
  7. 1 row in set (0.06 sec)

Number of male and female employees:

 
 
  1. MySQL (the best combination with PHP)> SELECT sex, COUNT (*) FROM mytable group by sex;
  2. + ------ + ---------- +
  3. | Sex | COUNT (*) |
  4. + ------ + ---------- +
  5. | F | 2 |
  6. | M | 1 |
  7. + ------ + ---------- +
  8. 2 row in set (0.00 sec)

Note that we use group by to group sex. The above content is an introduction to how to retrieve data from a MySQL database table.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.