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

Media query combined with rem layout in CSS3 to adapt to mobile screens

CSS3 syntax: (1rem = 100px for a 750px design) @m...

Analysis of the methods of visual structure layout design for children's websites

1. Warm and gentle Related address: http://www.web...

Solution to Docker's failure to release ports

Today I encountered a very strange situation. Aft...

A must-read career plan for web design practitioners

Original article, please indicate the author and ...

Thoughts on copy_{to, from}_user() in the Linux kernel

Table of contents 1. What is copy_{to,from}_user(...

How to use nodejs to write a data table entity class generation tool for C#

Although Microsoft provides T4 templates, I find ...

Sample code of uniapp vue and nvue carousel components

The vue part is as follows: <template> <...

Sample code for modifying the input prompt text style in html

On many websites, we have seen the input box disp...

Axios cancel request and avoid duplicate requests

Table of contents origin status quo Cancel reques...

Docker configuration Alibaba Cloud image acceleration pull implementation

Today I used docker to pull the image, but the sp...

Vue keeps the user logged in (various token storage methods)

Table of contents How to set cookies Disadvantage...

Example code for implementing verification code login in SMS API in Node

1. Node server setup + database connection The op...