Home  >  Article  >  Database  >  MySQL database query basics, simple query, conditional query, sorting query results

MySQL database query basics, simple query, conditional query, sorting query results

大家讲道理
大家讲道理Original
2017-08-19 10:48:381753browse

1. SELECT statement SELECT COL1, COL2, ....COLn FROM TABLE1, TABLE2, ....TABLEn

[WHERE CONDITIONS] -- Query conditions

[GROUP BY GROUP_BY_LIST] -- Query result grouping

[HAVING CONDITIONS] -- Query conditions - statistical results as conditions

[ORDER BY ORDER_LIST[ASC|DESC] -- Query result sorting

2. Simple query

1. Query all rows and columns in the table

eg: Query all rows and columns in the player table

select user_qq,user_name,user_sex,user_birthday,user_mobile from users;

select * from users;

2. Query some columns of the table

eg: Query from the player table Player QQ and nickname

select user_qq,user_name from users;

3. Use of alias

eg: Query player QQ and nickname from the player table and display it as ' Player QQ' and 'player nickname'

select user_qq as 'player QQ',user_name as 'player nickname' from users;

select user_qq 'player QQ',user_name 'player nickname' from users;

4.DISTINCT keyword-eliminate duplicate rows in the result set

eg: Display QQ of players who participated in the game, and require players who participated in multiple games not to display QQ repeatedly

select distinct user_qq from scores;

5.LIMIT keyword-specify the display range of the data in the result set

eg: Display the 3rd to 5th data in the player table

select * from users limit 2,3;

select*from users limit 3 ---Only display the first three pieces of data

三.Conditional query

1. Ordinary condition query

Syntax: SELECT COL_LIST FROM TABLE_NAME [WHERE CONDITION_EXPRESSION]

eg1: Query the player information with QQ number 12301

select * from users where user_qq =12301;

eg2: Query data with scores greater than 2500

select *from scores where score>2500;

<> --- --Not equal to >= -----Greater than or equal to <= -----Less than or equal to

eg3: Query the score information of the game number 1 and the score is greater than 4000 points

select * from scores where gno=1 and score>4000;

Logical operators: and-- and

               or-- or

                                                   

#eg4: Query the score information of game numbers 1 and 2

select * from scores where gno=1 or gno=2;

2. Fuzzy query

eg1: Query scores with scores between 2500 (inclusive) and 3000 (inclusive)

select *from scores where score>=2500 and score<=3000;

select * from scores where score between 2500 and 3000;

eg2: Query score information where score is not between 2500 (inclusive) and 3000 (inclusive)

select * from scores where score not between 2500 and 3000;

eg3: Query players born from January 1, 1987 to July 31, 1992

select * from users where user_birthday between '1987-01-01' and '1992-0731';

Wildcard: '_' One character Branch like 'L_'

Any length Route_Code Like 'AMS-%'

[] Within the specified range Airbusno Like 'AB0[1-5 ] '

[^^] Not in parentheses, Airbusno Like' Ab0 [^] '

Eg4: ​​Query all the player information of all the surnames of the Sun

## Select * from users where user_name like 'Sun%';

eg5: Query the information of all players whose surname is not Sun

select * from users where user_name not like 'Sun%';

3. Query empty Value operator

eg: Query the information of players whose birthday is null

select * from users where use_birthday is null;

eg: Query the players whose birthday is not NULL Information

select * from users where user_birthday is not null;

Four pairs of query results sorting

1. Sort the specified column (sort by , sorting method)

Syntax: SELECT CLO_LIST FROM TABLE_NAME ORDER BY ORDER_BY_LIST [ASC/DESC]

Example: Query all score information numbered 1 in the score table, and sort by score in ascending order

select *from scores where gno=1 order by score asc.

Example: Query all score information numbered 1 in the score table, and sort by score in descending order

select * from score where gno=1 order by score desc.

2. Sort multiple columns (sorting basis, sorting method, priority)

Example: Query all information in the score table , and sort in ascending order of game number and descending order of score

select * from scores order by gno asc, score desc

The above is the detailed content of MySQL database query basics, simple query, conditional query, sorting query results. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn