MySQL database SELECT query expression analysis

MySQL database SELECT query expression analysis

A large part of data management is searching, and SELECT occupies a large part of it.

SELECT select_expr [,select_expr...] 
[ 
  FROM table_reference 
  WHERE [where_condition] 
  [GROUP BY {col_name | position} [ASC | DESC],...] 
  [HAVING where_condition] 
  [ORDER BY {col_name | expr | position} [ASC | DESC],...] 
  [LIMIT {[offset,] row_count | row_count OFFSET offset}] 
]

So how do you write the select_expr query expression?

a. Each expression represents a desired column, there must be at least one
b. Multiple columns are separated by commas

In the user data table, execute to search only the first two columns

root@localhost test>select id,username from user;

Of course, the order of the query expressions may be inconsistent with the order in the data table. In this case, the query results will be displayed according to the results of the query expressions, that is, the order of the SELECT query expressions will affect the order of the query results.

root@localhost test>select username,id from user;

c. The asterisk (*) represents all columns, and table_name.* can represent all columns of the named table.

root@localhost test>SELECT * FROM user; 
root@localhost test>SELECT user.id,user.username FROM user;

Since the field name has been specified here, why do we need to specify the name of the data table user in user.id and use.name? Because if there is a multi-table connection, that is, two different tables have the same fields. If you write the field name directly, you may not be able to tell which data table the field belongs to. Therefore, you can distinguish which data table the field belongs to by adding the data table name.

d. A query expression can be given an alias using [AS] alias_name

root@localhost test>SELECT id AS userID,username AS Uname FROM user;

It is found that the original id and username in the table have become userID and Uname, so the alias will also affect the result.

Pay attention to the syntax of the alias here.

root@localhost test>SELECT id username FROM user;

At this time, username appears as an alias for id, that is, if the alias coincides with a real field in the data table, username now refers to the alias rather than the real field.

e. Aliases can be used in GROUP BY, ORDER BY or HAVING clauses

Summarize

The above is the MySQL database SELECT query expression parsing introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Sql query MySql database table name and description table field (column) information
  • Detailed explanation of MySQL database--multi-table query--inner join, outer join, subquery, correlated subquery
  • MySQL database advanced query and multi-table query
  • Why does the index in the Mysql database table not improve the query speed?
  • MySQL database query advanced multi-table query detailed explanation
  • Why is the query slow even though there is an index in the MySQL database table?

<<:  Detailed tutorial on installing mysql 5.7.26 on centOS7.4

>>:  How to install MySQL 5.7 on Ubuntu and configure the data storage path

Recommend

Solve the problems encountered when installing MySQL 8.0 on Win10 system

The problems and solutions encountered when insta...

Example of using #include file in html

There are two files a.htm and b.htm. In the same d...

Significantly optimize the size of PNG images with CSS mask (recommended)

This article is welcome to be shared and aggregat...

Detailed explanation of the middleman mode of Angular components

Table of contents 1. Middleman Model 2. Examples ...

Some suggestions for ensuring MySQL data security

Data is the core asset of an enterprise and one o...

Diving into JS inheritance

Table of contents Preface Prepare Summarize n way...

Docker adds a bridge and sets the IP address range

I don't know if it's because the binary d...

MySQL slow query pt-query-digest analysis of slow query log

1. Introduction pt-query-digest is a tool for ana...

Install multiple versions of PHP for Nginx on Linux

When we install and configure the server LNPM env...

Four ways to modify the default CSS style of element-ui components in Vue

Table of contents Preface 1. Use global unified o...

Detailed explanation of box-sizing in CSS3 (content-box and border-box)

Box-sizing in CSS3 (content-box and border-box) T...

MYSQL performance analyzer EXPLAIN usage example analysis

This article uses an example to illustrate the us...

How to quickly modify the root password under CentOS8

Start the centos8 virtual machine and press the u...