MySQL advanced statement (1)

1. MySQL advanced statement

The following two tables will be used in the next experiment

use KGCt;
create table location (Region char(20),Store_Name char(20));
insert into location values('East','Boston');
insert into location values('East','New York');
insert into location values('West','Los Angeles');
insert into location values('West','Houston');

create table store_info (Store_Name char(20),Sales int(10),Date char(10));
insert into store_info values('Los Angeles','1500','2020-12-05');
insert into store_info values('Houston','250','2020-12-07');
insert into store_info values('Los Angeles','300','2020-12-08');
insert into store_info values('Boston','700','2020-12-08');

insert image description here

1、select

Display all data records of one or several fields in the table

语法:SELECT "字段" FROM "表名";
SELECT Store_Name FROM Store_Info;

insert image description here

2、DISTINCT

Do not display duplicate data records

语法:SELECT DISTINCT "字段" FROM "表名";
SELECT DISTINCT Store_Name FROM Store_Info;

insert image description here

3. WHERE
conditional query statement

语法:SELECT "字段" FROM "表名" WHERE "条件";
SELECT Store_Name FROM Store_Info WHERE Sales > 1000;

insert image description here

4. AND OR
and or

语法:SELECT "字段" FROM "表名" WHERE "条件1" {[AND|OR] "条件2"}+ ;
SELECT Store_Name FROM Store_Info WHERE Sales > 1000 OR (Sales < 500 AND Sales > 200);

insert image description here

5. IN
shows the data record of the known value

语法:SELECT "字段" FROM "表名" WHERE "字段" IN ('值1', '值2', ...);
SELECT * FROM Store_Info WHERE Store_Name IN ('Los Angeles', 'Houston');

insert image description here

6. BETWEEN
displays data records within two value ranges

语法:SELECT "字段" FROM "表名" WHERE "字段" BETWEEN '值1' AND '值2';
SELECT * FROM Store_Info WHERE Date BETWEEN '2020-12-06' AND '2020-12-10';

insert image description here

7. Wildcards
Usually wildcards are used together with LIKE

% :百分号表示零个、一个或多个字符
_ :下划线表示单个字符

'A_Z':所有以 'A' 起头,另一个任何值的字符,且以 'Z' 为结尾的字符串。例如,'ABZ' 和 'A2Z' 都符合这一个模式,而 'AKKZ' 并不符合 (因为在 A 和 Z 之间有两个字符,而不是一个字符)。
'ABC%': 所有以 'ABC' 起头的字符串。例如,'ABCD' 和 'ABCABC' 都符合这个模式。
'%XYZ': 所有以 'XYZ' 结尾的字符串。例如,'WXYZ' 和 'ZZXYZ' 都符合这个模式。
'%AN%': 所有含有 'AN'这个模式的字符串。例如,'LOS ANGELES' 和 'SAN FRANCISCO' 都符合这个模式。
'_AN%':所有第二个字母为 'A' 和第三个字母为 'N' 的字符串。例如,'SAN FRANCISCO' 符合这个模式,而 'LOS ANGELES' 则不符合这个模式。

8. LIKE
matches a pattern to find the data record we want

语法:SELECT "字段" FROM "表名" WHERE "字段" LIKE {模式};
SELECT * FROM Store_Info WHERE Store_Name like '%os%';

insert image description here

9. ORDER BY
sorted by keywords

语法:SELECT "字段" FROM "表名" [WHERE "条件"] ORDER BY "字段" [ASC, DESC];
#ASC 是按照升序进行排序的,是默认的排序方式。
#DESC 是按降序方式进行排序。
SELECT Store_Name,Sales,Date FROM Store_Info ORDER BY Sales DESC;

insert image description here

9. Function

(1) Mathematical functions:

function meaning
abs(x) returns the absolute value of x
rand() Returns a random number between 0 and 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) Rounded value with x to y decimal places
sqrt(x) returns the square root of x
truncate(x,y) Returns the value of the number x truncated to y decimal places
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, and can also return the largest value of multiple fields
least(x1,x2…) Returns the smallest value in the collection, and can also return the smallest value of multiple fields
SELECT abs(-1), rand(), mod(5,3), power(2,3), round(1.89);
SELECT round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1), least(1.89,3,6.1,2.1);

insert image description here

(2) Aggregation function:

function meaning
avg() Returns the mean of the specified column
count() Returns the number of non-NULL values ​​in the specified column
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 Store_Info;
SELECT count(Store_Name) FROM Store_Info;
SELECT count(DISTINCT Store_Name) FROM Store_Info;
SELECT max(Sales) FROM Store_Info;
SELECT min(Sales) FROM Store_Info;
SELECT sum(Sales) FROM Store_Info;

insert image description here

insert image description here

count(*) includes the number of rows of all columns, and will not ignore the column value NULL when counting the results

count(column name) only includes the number of rows in the column with the column name. When counting the results, rows whose column value is NULL will be ignored

(3) String function:

function meaning
trim() Returns the value with the specified format removed
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) Gets a string of length z starting at position y in string x
length(x) Returns the length of the string x
replace(x,y,z) substitute the string z for the string y in the string x
upper(x) convert all letters of the string x to uppercase
lower(x) Convert all letters of the string x to lowercase
left(x,y) Returns the first y characters of the string x
right(x,y) Returns the last y characters of the string x
repeat(x,y) repeat the string x y times
space(x) returns x spaces
strcmp(x,y) Compare x and y, the returned value can be -1,0,1
reverse(x) reverse the string x
SELECT concat(Region, Store_Name) FROM location WHERE Store_Name = 'Boston';  
#如sql_mode开启了PIPES_AS_CONCAT,"||"视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
SELECT Region || ' ' || Store_Name FROM location WHERE Store_Name = 'Boston';
SELECT substr(Store_Name,3) FROM location WHERE Store_Name = 'Los Angeles';
SELECT substr(Store_Name,2,4) FROM location WHERE Store_Name = 'New York';
SELECT TRIM ([ [位置] [要移除的字符串] FROM ] 字符串);
#[位置]:的值可以为 LEADING (起头), TRAILING (结尾), BOTH (起头及结尾)。 
#[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。
SELECT TRIM(LEADING 'Ne' FROM 'New York');
SELECT Region,length(Store_Name) FROM location;
SELECT REPLACE(Region,'ast','astern')FROM location;

insert image description here

insert image description here

2. Advanced statement query

2.1GROUP BY

Summarize and group the query results of the fields behind GROUP BY, which is usually used in combination with aggregation functions. There is
a principle in GROUP BY that all fields that appear after GROUP BY must appear after SELECT;
all fields that appear after SELECT and Fields that do not appear in aggregate functions must appear after GROUP BY

语法:SELECT "字段1", SUM("字段2") FROM "表名" GROUP BY "字段1";
SELECT Store_Name, SUM(Sales) FROM Store_Info GROUP BY Store_Name ORDER BY sales desc;

insert image description here

2.2HAVING

Used to filter the recordset returned by the GROUP BY statement, usually used in conjunction with the GROUP BY statement. The
existence of the HAVING statement makes up for the deficiency that the WHERE keyword cannot be used in conjunction with the aggregate function.

语法:SELECT "字段1", SUM("字段2") FROM "表格名" GROUP BY "字段1" HAVING (函数条件);
SELECT Store_Name, SUM(Sales) FROM Store_Info GROUP BY Store_Name HAVING SUM(Sales) > 1500;

insert image description here

2.3 Aliases

field alias table alias

语法:SELECT "表格別名"."字段1" [AS] "字段別名" FROM "表格名" [AS] "表格別名";
SELECT A.Store_Name Store, SUM(A.Sales) "Total Sales" FROM Store_Info A GROUP BY A.Store_Name;

insert image description here

2.4 Subqueries

Join tables, insert another SQL statement in WHERE clause or HAVING clause

语法:SELECT "字段1" FROM "表格1" WHERE "字段2" [比较运算符] 				#外查询
(SELECT "字段1" FROM "表格2" WHERE "条件");									#内查询
#可以是符号的运算符,例如 =、>、<、>=、<= ;也可以是文字的运算符,例如 LIKE、IN、BETWEEN
SELECT SUM(Sales) FROM Store_Info WHERE Store_Name IN 
(SELECT Store_Name FROM location WHERE Region = 'West');

SELECT SUM(A.Sales) FROM Store_Info A WHERE A.Store_Name IN 
(SELECT Store_Name FROM location B WHERE B.Store_Name = A.Store_Name);

insert image description here

insert image description here

2.5EXISTS

It is used to test whether the inner query produces any results, similar to whether the Boolean value is true
#If yes, the system will execute the SQL statement in the outer query. If not, then the entire SQL statement will not produce any results.

语法:SELECT "字段1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
SELECT SUM(Sales) FROM Store_Info WHERE EXISTS (SELECT * FROM location WHERE Region = 'West');

insert image description here

Guess you like

Origin blog.csdn.net/weixin_51728919/article/details/131374887