Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use select statement to query table in MySQL database

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to use select statements to query tables in the MySQL database. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

MySQL database uses select statement to query how tables are recorded.

First, let's take a look at the basic syntax of the select statement, which is as follows:

Selectselection_list selects those columns fromtable_list selects rows from that table conditions that whereprimary_constraint rows must meet groupbygrouping_columns on how the results are grouped the second condition that havingsecondary_constraint rows must meet orderbysorting_colums on how to sort the results limitcount result qualification

Note: all keywords used must be given exactly in the above order. For example, a having clause must follow the grougby clause and the orderby clause.

1. General inquiry

1. Retrieve all the data in the table

Mysql > select*frompet

Note: retrieve all the data in the pet table

two。 Query specific rows

Mysql > select*frompetwherename= "Bowser"

Note: retrieve the row of data in the pet table where name equals "Bowser"

3. Query specific columns

Selectname,birthfrompetwhereowner= "Gwen"

Note: in the pet table, select the row where owner is "Gwen" and only retrieve the name and birth columns of this row.

2. Conditional inquiry

1. Greater than / less than condition (, =)

Mysql > select*frompetwhereage > = "5"

Note: in the pet table, retrieve all data with age greater than or equal to 5

two。 Combination condition (and/or)

Mysql > select*frompetwhereage > = 5andsexes = "f"

Note: in the pet table, retrieve all data whose age is greater than or equal to 5 and sex is "f".

Mysql > selectfrom*frompetwhereage > = "5" orsex= "f"

Note: in the pet table, retrieve all data whose age is greater than or equal to 5 or sex is "f"

Mysql > select*frompetwhere (age > "5" andsex= "f") or (name= "tom" andsex= "m")

Note: in the pet table, all data with age greater than 5 and sex is "f" or name is "tom" and sex is "m" are detected

MySQL database uses select statement to query how tables are recorded.

Third, query sorting

The format is: orderbycolumn_ name [ASC / desc] [...]

Note: asc indicates ascending order, which is the default value, and desc is descending order. Orderby cannot be sorted by text,text and image data types.

Mysql > selectname,birthfrompetorderbybirth

Note: in the pet table, the columns name and birth are retrieved and sorted in ascending order by birth.

Mysql > selectname,birthfrompetorderbybirthdesc

Note: in the pet table, the columns name and birth are retrieved and sorted by birth in descending order.

Mysql > selectname,agefrompetorderbyname,birth

Note: in the pet table, retrieve the data of these two columns of name and age, and sort by name first, and then sort by birth.

After reading the above, do you have any further understanding of how to use select statements to query tables in MySQL databases? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report