MySQL database advanced SQL statement
SELECT TRIM
SELECT TRIM (position'the character you want to remove' from'original character');
#[position]: The value can be LEADING (beginning), TRAILING (end), BOTH (beginning and end).
#[I want to remove the character]: The character string removed from the beginning, end, or beginning and end of the string. The default is a space.
select trim(leading '.' from '..abc..');
select trim(trailing '.' from '..abc..');
select trim(both '.' from '..abc..');
Connect query
修改下表内数据,方便观察
update tickets set Store_Name='hunan' where Sales=500;
insert into city (Region,Name) values('south','fujian');
insert into tickets(Store_Name,Sales,Date) values('chengdu','2000','2021-2-01');
inner join (equal value join): Only return rows with equal join fields in the two tables.
Syntax: SELECT field FROM table 1 INNER JOIN table 2 ON table 1. field = table 2. field;
left join (left join): return including all the records in the left table and the records in the right table where the join field is equal.
Syntax: SELECT field FROM table 1 LEFT JOIN table 2 ON table 1. field = table 2. field;
right join (right join): return including all records in the right table and the records in the left table that are equal to the join field.
Syntax: SELECT field FROM table 1 RIGHT JOIN table 2 ON table 1. field = table 2. field;
select * from city A inner join tickets B on A.Name = B.Store_Name;
select * from city A left join tickets B on A.Name = B.Store_Name;
select * from city A right join tickets B on A.Name = B.Store_Name;
CREATE VIEW
Views can be treated as virtual tables or stored queries.
The difference between the view and the table is that the table has the actual stored data, and the view is a structure built on the table, and it does not actually store the data.
The temporary table automatically disappears after the user exits or the connection with the database is disconnected, and the view does not disappear.
The view does not contain data, only its definition is stored, and its purpose is generally to simplify complex queries. For example, if you want to connect and query several tables, but also perform statistical sorting and other operations, it will be very troublesome to write SQL statements. Use a view to connect several tables, and then perform a query operation on this view, just like a table The query is the same, very convenient.
Syntax: CREATE VIEW view table name AS SELECT statement;
select A.Region,sum(B.Sales) from city as A inner join tickets as B on A.Name = B.Store_Name group by Region;
create view C as select A.Region,sum(B.Sales) from city as A inner join tickets as B on A.Name = B.Store_Name group by Region;
UNION
Union, combining the results of two SQL statements, the fields generated by the two SQL statements need to be the same data type
UNION: The data value of the generated result will not be repeated, and it will be sorted according to the order of the fields.
Syntax: [SELECT statement 1] UNION [SELECT statement 2];
select Region from city union select Store_Name from tickets;
UNION ALL: List all the data values of the generated results, regardless of whether there are duplicates.
Syntax: [SELECT statement 1] UNION ALL[SELECT statement 2];
select Region from city union all select Store_Name from tickets;
Intersection value
Modify the table structure and data records for easy observation
alter table city change Name Store_Name char(20);
delete from city where Store_Name='fujian';
insert into tickets(Store_Name,Sales,Date) values('chengdu',2000,'2021-2-01');
Take the intersection of the results of two SQL statements
select A.Store_Name from city as A inner join tickets as B on A.Store_Name = B.Store_Name;
select A.Store_Name from city as A inner join tickets as B using(Store_Name);
select Store_Name from city union all select Store_Name from tickets;
select Store_Name from(select Store_Name from city union all select Store_Name from tickets) A group by A.Store_Name having count(*) > 1;
When there are no duplicate rows in the two tables, and there is indeed an intersection
select Name from (select Name from city union all select Store_Name from tickets) A group by A.Name having count(*) > 1;
Supplementary note:
Take the intersection of the results of the two SQL statements without duplication
select Store_Name from(select A.Store_Name from city A inner join tickets B on A.Store_Name =B.Store_Name) C group by C.Store_Name;
select distinct A.Store_Name from city as A inner join tickets as B using(Store_Name);
select distinct A.Store_Name from city as A inner join tickets as B using(Store_Name);
select distinct Store_Name from city where (Store_Name) in (select Store_Name from tickets);
select distinct A.Store_Name from city A left join tickets B using(Store_Name) where B.Store_Name is not null;
No intersection value
Display the result of the first SQL statement, and the result of no intersection with the second SQL statement, and no duplication
select distinct Store_Name from tickets where(Store_Name) not in (select Store_Name from city);
select A.*,B.* from tickets A left join city B using(Store_Name);
select Store_Name from tickets A left join city B using(Store_Name) where B.Store_Name is null;
select distinct Store_Name from tickets A left join city B using(Store_Name) where B.Store_Nameore_Name is null;
CASE
SQL is used as a keyword for logic such as IF-THEN-ELSE.
Syntax:
SELECT CASE ("column name")
WHEN "Condition 1" THEN "Result 1"
WHEN "Condition 2" THEN "Result 2"
…
[ ELSE "Result N"]
END
FROM "Table name";
The "condition" can be a value or a formula. The ELSE clause is not required.
select case Store_Name
when 'beijing' then Sales + 1000
when 'shanghai' then Sales - 1000
else Sales * 10
end
TEST,Store_Name
from tickets;