Article Directory
- First, the query of the connection table
- Two, CREATE VIEW ---- view
- Three, UNION ---- joint set
- 4. Intersection value - take the intersection of the results of two SQL statements
- Five, no intersection value
- 6. CASE
- Seven, the difference between empty value (NULL) and no value ('')
- Eight, regular expressions ----
First, the query of the connection table
Inner join (inner join): returns only rows with equal join fields in the two tables
left join (left join): Returns all records including all records in the left table and records with the same join field in the right table
right join (right join): Returns all records including all records in the right table and records with the same join field in the left table
#内连接:
SELECT * FROM location A RIGHT JOIN Store_Info B on A.Store_Name = B.Store_Name ;
#左连接
SELECT * FROM location A LEFT JOIN Store_Info B on A.Store_Name = B.Store_Name ;
#右连接
SELECT * FROM location A RIGHT JOIN Store_Info B on A.Store_Name = B.Store_Name ;
Other inner join methods:
SELECT * FROM location A INNER JOIN Store_Info B on A.Store_Name = B.Store_Name ;
SELECT * FROM location A, Store_Info B WHERE A.Store_Name = B.Store_Name;
Two, CREATE VIEW ---- view
The difference between a view and a table is that the table actually stores data records, while the view is a structure built on the table, and it does not actually store data records.
Temporary tables disappear automatically after the user logs out or the connection to the database is disconnected, but the view does not disappear.
A view does not contain data, only its definition is stored, and its use generally simplifies complex queries. For example, if you want to perform join queries on several tables, and also perform statistical sorting operations, it will be very troublesome to write SQL statements. Using a view to join several tables, and then performing query operations on this view is the same as performing operations on one table. Queries are the same, very convenient.
语法:CREATE VIEW "视图表名" AS "SELECT 语句";
CREATE VIEW V_REGION_SALES AS SELECT A.Region REGION,SUM(B.Sales) SALES FROM location A
INNER JOIN Store_Info B ON A.Store_Name = B.Store_Name GROUP BY REGION;
SELECT * FROM V_REGION_SALES;
DROP VIEW V_REGION_SALES;
Three, UNION ---- joint set
Merge the results of two SQL statements, the fields generated by the two SQL statements need to be of the same data record type
UNION: the data record values of the generated results will not be repeated, and will be sorted according to the order of the fields
语法:[SELECT 语句 1] UNION [SELECT 语句 2];
UNION ALL :将生成结果的数据记录值都列出来,无论有无重复
语法:[SELECT 语句 1] UNION ALL [SELECT 语句 2];
SELECT Store_Name FROM location UNION SELECT Store_Name FROM Store_Info;
SELECT Store_Name FROM location UNION ALL SELECT Store_Name FROM Store_Info;
4. Intersection value - take the intersection of the results of two SQL statements
SELECT A.Store_Name FROM location A INNER JOIN Store_Info B ON A.Store_Name = B.Store_Name;
SELECT A.Store_Name FROM location A INNER JOIN Store_Info B USING(Store_Name);
#取两个SQL语句结果的交集,且没有重复
SELECT DISTINCT A.Store_Name FROM location A INNER JOIN store_info B USING(Store_Name);
SELECT DISTINCT Store_Name FROM location WHERE (Store_Name) IN (SELECT Store_Name FROM store_info);
SELECT DISTINCT A.Store_Name FROM location A LEFT JOIN store_info B USING(Store_Name) WHERE B.Store_Name IS NOT NULL;
SELECT A.Store_Name FROM (SELECT B.Store_Name FROM location B INNER JOIN Store_Info C ON B.Store_Name = C.Store_Name) A
GROUP BY A.Store_Name;
SELECT A.Store_Name FROM
(SELECT DISTINCT Store_Name FROM location UNION ALL SELECT DISTINCT Store_Name FROM store_info) A
GROUP BY A.Store_Name HAVING COUNT(*) > 1;
Five, no intersection value
Display the results of the first SQL statement, and the results that do not overlap with the second SQL statement, and there is no duplication
SELECT DISTINCT Store_Name FROM location WHERE (Store_Name) NOT IN (SELECT Store_Name FROM store_info);
SELECT DISTINCT A.Store_Name FROM location A LEFT JOIN store_info B USING(Store_Name) WHERE B.Store_Name IS NULL;
SELECT A.Store_Name FROM
(SELECT DISTINCT Store_Name FROM location UNION ALL SELECT DISTINCT Store_Name FROM store_info) A
GROUP BY A.Store_Name HAVING COUNT(*) = 1;
6. CASE
It is a keyword used by SQL as logic such as IF-THEN-ELSE
语法:
SELECT CASE ("字段名")
WHEN "条件1" THEN "结果1"
WHEN "条件2" THEN "结果2"
...
[ELSE "结果N"]
END
FROM "表名";
#"条件" 可以是一个数值或是公式。 ELSE 子句则并不是必须的。
SELECT Store_Name, CASE Store_Name
WHEN 'Los Angeles' THEN Sales * 2
WHEN 'Boston' THEN 2000
ELSE Sales
END
"New Sales",Date
FROM Store_Info;
#"New Sales" 是用于 CASE 那个字段的字段名。
Seven, the difference between empty value (NULL) and no value ('')
1. The length of no value is 0, which does not take up space; while the length of NULL value is NULL, which takes up space.
2. IS NULL or IS NOT NULL is used to judge whether the field is NULL or not NULL, and cannot find out whether it has no value.
3. Use ='' or <>'' to handle the judgment of no value. <> represents not equal to.
4. When using count() to specify the number of rows in the field statistics, if a NULL value is encountered, it will be automatically ignored, and if no value is encountered, it will be added to the record for calculation.
)
SELECT length(NULL), length(''), length('1');
SELECT * FROM City WHERE name IS NULL;
SELECT * FROM city WHERE name IS NOT NULL;
SELECT * FROM city WHERE name = '';
SELECT * FROM city WHERE name <> '';
SELECT COUNT(*) FROM city;
SELECT COUNT(name) FROM city;
Eight, regular expressions ----
match pattern | describe | example |
---|---|---|
^ | start character of matching text | '^bd' matches a string starting with bd |
$ | Matches the end character of the text | 'qn$' matches strings ending with qn |
. | matches any single character | 's.t' matches any string with a character between s and t |
* | matches zero or more of the characters preceding it | 'fo*t' matches t preceded by any o |
+ | Matches the preceding character 1 or more times | 'hom+' matches a string starting with ho followed by at least one m |
string | matches the specified string | 'clo' matches strings containing clo |
p1 | p2 | matches p1 or p2 |
[…] | Matches any character in the character set | '[abc]' matches a or b or c |
[^…] | matches any character not in parentheses | '[^ab]' matches a string that does not contain a or b |
{n} | Match the preceding string n times | 'g{2}' matches a string containing 2 g |
{n,m} | Matches the preceding string at least n and at most m times | 'f{1,3}' matches f at least 1 time and at most 3 times |
grammar:
SELECT "字段" FROM "表名" WHERE "字段" REGEXP {模式};
SELECT * FROM store_info WHERE Store_Name REGEXP 'os';
SELECT * FROM store_info WHERE Store_Name REGEXP '\^[A-G]';
SELECT * FROM store_info WHERE Store_Name REGEXP 'Ho|Bo';