MySQL database advanced SQL statement (1)

Ready to work

use test;
create table city (Region char (20),Name char(20));
insert into city values('south','shanghai');
insert into city values ('south','hunan');
insert into city values ('north','beijing');
insert into city values ('north','tianjin');

create table tickets (Store_Name char(20),Sales int(10),Date char(10));
insert into tickets values('beijing','1250','2021-1-28');
insert into tickets values ('tianjin','750','2021-1-29');
insert into tickets values('beijing','500','2021-1-30');
insert into tickets values('shanghai','1800','2021-1-31');

Insert picture description here
Insert picture description here

One, MySQL advanced SQL statement

SELECT

Display all data in one or several fields in the table
Syntax: SELECT “field” FROM “table name”;

例:select Store_Name FROM tickets;

Insert picture description here

DISTINCT

Do not display duplicate data.
Syntax: SELECT DISTINCT “field” FROM “table name”;

例:select distinct Store_Name FROM tickets;

Insert picture description here

WHERE

Conditional query
Syntax: SELECT "field" FROM "table name" WHERE "condition";

例:select Store_Name FROM tickets where Sales > 1500;

Insert picture description here

AND OR

And or
Syntax: SELECT "field" FROM "table name" WHERE "condition 1" {[AND | OR] "condition 2"}+;

例:select Store_Name FROM tickets where Sales > 1500 or (Sales < 1000 and Sales > 500);

Insert picture description here

IN

Display the data of the known value
Syntax: SELECT “field” FROM “table name” WHERE “field” IN ('value 1','value 2', …);

select * from tickets where Store_Name in('tianjin','shanghai');

Insert picture description here

BETWEEN

Display the data in two value ranges.
Syntax: SELECT "field" FROM "table name" WHERE "field" BETWEEN'value 1'AND'value 2';

select * from tickets where Date between '2021-1-29' and '2021-1-31';

Insert picture description here

Wildcard

Usually wildcards are used together with LIKE
%: Percent sign means zero, one or more characters
_: Underscore means a single character'A_Z
': all characters starting with'A' and another character with any value, and beginning with ' Z'is the ending character string. For example,'ABZ' and'A2Z' both conform to this pattern, but'AKKZ' does not (because there are two characters between A and Z, not one).
'ABC%':: All strings starting with'ABC'. For example, both'ABCD' and'ABCABC' conform to this pattern.
'%XYZ': All strings ending with'XYZ'. For example, both'WXYZ' and'ZZXYZ' conform to this pattern.
'%AN%': All strings containing the pattern'AN'. For example,'LOS ANGELES' and'SAN FRANCISCO' both conform to this model.
'_AN%': All strings with the second letter being'A' and the third letter being'N'. For example,'SAN FRANCISCO' fits this model, but'LOS ANGELES' does not fit this model.

LIKE

Match a pattern to find the data we want.
Syntax: SELECT "field" FROM "table name" WHERE "field" LIKE {pattern};

select * from tickets where Store_Name like '%an%';

Insert picture description here

ORDER BY

Sort by keywords
Syntax: SELECT "column" FROM "table name" [WHERE "condition"] ORDER BY "column" [ASC,DESC];
#ASC is sorted in ascending order, which is the default sorting method.
#DESC is sorted in descending order.

select Store_Name,Sales,Date from  tickets order by Sales desc;
select Store_Name,Sales,Date from  tickets order by Sales asc;

Insert picture description here

Two, function

Mathematical function

abs(x) Returns the absolute value of x
rand() Returns a random number from 0 to 1
mod (x, y) Returns the remainder after dividing x by y
power (x, y) Returns x to the power of y
round(x) Returns the integer closest to x
round(x,y) Keep x's y decimal place after rounding
sqrt(x) Returns the square root of x
truncate(x,y) Returns the value of the number x truncated to y decimal places (only the y digits after the decimal point are kept)
ceil(x) Returns the smallest integer greater than or equal to x
floor(x) Returns the largest integer less than or equal to x
greatest(x1,x2…) Returns the largest value in the collection
least(x1,x2…) Return the smallest value in the set

Example:
Insert picture description here
Insert picture description here
Insert picture description here

Aggregate function

avg() Returns the average value of the specified column
count() Returns the number of non-NULL values ​​in the specified column (non-empty row number)
min () Returns the minimum value of the specified column,
max() Returns the maximum value of the specified column
sum(x) Returns the sum of all values ​​in the specified column
例:
select avg(Sales) from tickets;
select count(Sales) from tickets;
select min(Sales) from tickets;
select max(Sales) from tickets;
select sum(Sales) from tickets;

Insert picture description here

String function

trim() Return the value without the specified format
concat(x,y) Concatenate the provided parameters x and y into a string
substr(x,y) Get the string starting from the yth position in the string x, which has the same effect as the substring() function
substr(x,y,z) Get a string of length z starting from the yth position in the string x
length(x) Returns the length of string x
replace(x,y,z) Replace string z with string y in string x
upper(x) Change all letters of string x to uppercase letters
lower(x) Change all letters of string x to lowercase letters
left(x,y) Returns the first y characters of string x
right(x,y) Returns the last y characters of the string x
repeat (x, y) Repeat string x y times
space(x) X spaces are returned
strcmp(x,y) Compare x and y, the returned value can be -1,0,1
reverse(x) Reverse string x

Example:

select trim(leading 'ab' from 'abcde');
select trim(trailing 'de' from 'abcde');
select trim(both 'a' from 'abcde');
select trim(both 'e' from 'abcde');

**Bold style**

select concat(Region,Name) from city where Region = 'north';

Insert picture description here

select substr(Store_Name,5,2) from tickets;

Insert picture description here

select length(Name) from city;

Insert picture description here

select replace(Region,'th','abc') from city;

Insert picture description here

select upper(Name) from city;

Insert picture description here

select lower(ABCDE);

Insert picture description here

select left(Name,2) from city;
select right(Name,2) from city;

Insert picture description here

select repeat(Name,3) from city;

Insert picture description here

select space(3);

Insert picture description here

select strcmp(10,50);
select strcmp(50,10);
select strcmp(10,10);

Insert picture description here

select reverse(Region) from city;

Insert picture description here

To be continued...

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/113849677