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

The difference between absolute path and relative path in web page creation

1. Absolute path First of all, on the local compu...

Detailed steps for running springboot project in Linux Docker

Introduction: The configuration of Docker running...

Example of how to deploy a Django project using Docker

It is also very simple to deploy Django projects ...

JavaScript quickly implements calendar effects

This article example shares the specific code of ...

Docker Gitlab+Jenkins+Harbor builds a persistent platform operation

CI/CD Overview CI workflow design Git code versio...

MySQL 5.7 installation-free configuration graphic tutorial

Mysql is a popular and easy-to-use database softw...

How to build your own Angular component library with DevUI

Table of contents Preface Creating a component li...

Analysis and solutions to problems encountered in the use of label tags

I used the label tag when I was doing something re...

How to remove the header from the element table

Document hints using the show-header attribute sh...

Two ways to implement square div using CSS

Goal: Create a square whose side length is equal ...

Detailed explanation of the API in Vue.js that is easy to overlook

Table of contents nextTick v-model syntax sugar ....

About Docker security Docker-TLS encrypted communication issues

Table of contents 1. Security issues with Docker ...

Detailed explanation of the use of stat function and stat command in Linux

stat function and stat command Explanation of [in...