SQL basic query and conditional query

After finishing my graduation thesis, I finally have time to review sql, and continue my journey of learning ~ next time I can't be the same as last time, I forgot all the simple sql statements.

1. Deduplication of query results keyword distinct

 

select distinct university from user_profile;

2. Rename the queried column keyword as keyword limit 

The search result returns the 0 after the two limit can be omitted (index, number)

select device_id as user_infors_example from user_profile limit 0,2;

3. Search and sort keyword order by  

ASC is an abbreviation of ascend ascending (ascending order) DESC is an abbreviation of descending descending (descending order)

select device_id,age from user_profile order by age ASC;

4. When searching for a data segment, the keyword can be where or between and

 

select device_id,gender,age from user_profile where age between 20 and 23

5. Exclude some users' information keywords not in

 

select device_id,gender,age,university from user_profile where university not in ('复旦大学')

5. Fuzzy query of keywords keyword like

 

select device_id,age,university from user_profile where university like '北京%'

Supplement on fuzzy query

_: matches any character

%: Match 0 or more characters

[ ]: Match any character in [ ]

[^ ]: Does not match any of the characters

eg1: Query the detailed information of the student whose surname is 'Zhang' in the student table.

SELECT * FROM 学生表 WHERE 姓名 LIKE ‘张%’

eg2: Query the situation of the students whose surnames are 'Zhang', 'Li' and 'Liu' in the student table.

SELECT * FROM 学生表 WHERE 姓名 LIKE '[张李刘]%’

eg3: Query the name and student number of the student whose first name in the student table is "small" or "big".

SELECT 姓名,学号 FROM 学生表 WHERE 姓名 LIKE '_[小大]%'

eg4: Query all students whose surname is not "Liu" in the student table.

SELECT 姓名 FROM 学生 WHERE 姓名 NOT LIKE '刘%’

eg5: Query the student information whose last digit of the student number is not 2, 3, or 5 from the student table.

SELECT * FROM 学生表 WHERE 学号 LIKE '%[^235]'

Because % can match one or more digits, [^ ] means no match

Guess you like

Origin blog.csdn.net/weixiwo/article/details/130016906