Java novice learning guide [day26] --- MySQL advanced

1. Multi-table query

Query two or more tables

1. Cartesian product (Avoid Cartesian product in actual query)

Assuming set A={a,b} and set B={0,1,2}, then the Cartesian product of the two sets is {(a,0),(a,1),(a,2),( b,0),(b,1),(b,2)}; is the combination between all sets

Elimination of Cartesian product needs to pass the valid conditions of WHERE

2. Internal connection: if there is no corresponding relationship, it will not be displayed

隐式内连接:SELECT  查询内容 FROM1,2 WHERE 消除笛卡尔积的条件;
显示内连接:SELECT  查询内容 FROM1 [INNER] JOIN2 ON 消除笛卡尔积的条件;

3. External connection

左外连接:左边表的内容均会显示
SELECT	<selectList>
	FROM A  LEFT JOIN B
	ON A.column_name = B.column_name;
右外连接:右边表的内容均会显示
SELECT	<selectList>
	FROM A  RIGHT JOIN B
	ON A.column_name = B.column_name;

4. Subquery: Is the condition after WHERE in the SELECT query statement uncertain, or is it composed of a SELECT query statement

需求:查询零售价比罗技MX1100更高的所有商品信息:
SELECT * FROM product WHERE salePrice > (SELECT salePrice FROM product WHERE productName = '罗技MX1100')

5. Self-join; if you look at a table as two to do a query, you must take an alias. Self-join is actually a subquery

Insert picture description here

/*查询表中所有员工以及所在部门的经理姓名*/
SELECT emp.id,emp.name,manager.name 主公
FROM employee2 emp
LEFT JOIN employee2 manager ON emp.manager_id = manager.id

2. Data operation (addition, deletion and modification)

1、插入数据
INSERT INTO 表名 (列名,列名,...) VALUES (,,...);
INSERT INTO 表名 (列名,列名,...) VALUES (,,...),(,,...);
注意:一般不会添加主键。
2、修改数据
UPDATE 表名 SET 列名=,列名=,... WHERE ....
注意:一般都需要加上WHERE条件,否则全表的数据都会被修改。
3、删除数据
DELETE FROM 表名 WHERE ....
注意:如果省略了where子句,则全表的数据都会被删除

3. MySQL functions

1. Aggregate function

COUNT: the number of records of the statistical result, if the column value is NULL, it will not be counted, that is, the NULL value will be ignored

MAX: statistical calculation maximum

MIN: Statistical calculation minimum

SUM: Statistical calculation sum

AVG: Calculate the average value of statistics, if the column value is NULL, it will not be counted, that is, the NULL value will be ignored

2. Type quasi conversion function

CAST(value AS type), CONVERT(value, type): used to obtain a value of one type and generate a value of another type;

(1)SELECT CAST('123abc' AS signed) + 1; --> 124
(2)SELECT CONVERT('123abc', signed) + 2; --> 125

3. String functions

concat(): splicing

lenght(): length

upper(): turn to uppercase

lower(): turn lowercase

trim(): remove spaces

4. Date function

now() current date (year, month, day, hour, minute, second)

CURDATE(): current date (year, month, day)

5. Control flow function

if(test,val1,val2) ------>Similar to trinocular operation, test: judgment condition judgment condition is true, return val1 judgment condition is false, return val2

ifnull(val1,val2) -------->val1 is not empty, return val1 val1 is empty, return val2

4. Data backup and recovery

1. In the dos command line window, if the operating system version is higher, use the administrator mode

Backup: mysqldump -u database name -p password table name> absolute address

Recovery: mysqldump -u database name -p password table name <absolute address

2. Use visualization tools to store sql files/run sql files

Guess you like

Origin blog.csdn.net/WLK0423/article/details/109918309
Recommended