Detailed explanation of Mysql keywords

1. terminated by import and export separator

Export a table. Fields are separated by | and enclosed by ".

select * from comment into outfile 'D:/working/ttt.txt' fields terminated by '|' enclosed by '"';
select * from p_policy into outfile 'c:/a.txt' fields terminated by '|';
导入一份数据倒表中
load data infile "D:/working/ttt.txt" into table p_policy fields terminated by "|";

2.CALL is used to call stored procedures

如:CALL showUser()

3. CURRENT_DATE is used to get the current date

如“SELECT CURRENT_DATE”返回的就是当天的日期

4. CURSOR is used to declare the cursor in mysql, which is used in the stored procedure and acts as a pointer

用法:“DECLARE 游标名称 CURSOR FRO 查询出的结果集”

5.DISTINCT to remove duplicate data

Only deduplicated data can be returned, which must be the first field in the query. It is often used to query how many unique data are in a certain field.

SELECT COUNT(DISTINCT name) FROM user

EXPLAIN is used to check the execution of a sql, whether it has added an index or whether it has done a full table scan, etc.

使用方式:在SELECT前加上,如EXPLAIN SELECT * FROM user

6. FULLTEXT full-text index for optimizing query speed

可以在创建表时加入FULLTEXT(字段1, 字段2),也可以在创建表之后单独加入ALTER TABLE 表名 ADD FULLTEXT INDEX 索引名 (字段名)需要注意的是添加索引是,表引擎要使用MyISAM不然会报错The used table type doesn't support FULLTEXT indexes

7. MOD remainder function

如:select mod(12,5)返回余数2

8. RLIKE fuzzy query

When it is different from like, the content of rlike can be regular, and can not match exactly

如:select * from user where email RLIKE '^www.*$';

9. XOR stands for Logical Exclusive Or

, when any operand is NULL, the return value is NULL. For non-NULL operands, if the two logical true and false values ​​are different, the return result is 1, otherwise it is 0

如:select true XOR null;select true XOR true;select true XOR false;

10. USING connection

When querying, if the field names are the same, it can be used as a connection condition, and using can appear instead of on

如:select * from user left join students USING(quanxian)

11. WHILE loop statement keywords

Used in stored procedures, it needs to be used in conjunction with flow control statements

如:CREATE PROCEDURE fun() BEGIN SET @sum:=10; WHILE @sum > 0 DO SELECT @sum; SET @sum:=@sum-1; END WHILE; END CALL fun(); DROP PROCEDURE fun

12. SEPARATOR can concatenate the query results into one line with strings

SEPARATOR specifies the connector, which needs to be used with GROUP BY

如:SELECT *, GROUP_CONCAT(username order by username separator ';') SCORE FROM user GROUP BY email RLIKE '^www.*$'

13. RIGHT truncates strings

如:select RIGHT('这是一个字符串',5),会倒序截取指定位数的字符串

14.REPEAT copies the given data as a string for the specified number of times

如:select REPEAT(3,3)得出的结果为“333

15.RENAME modify table name

使用:rename table user2 to user3;

16.NATURAL natural connection

It is a type of JOIN, which is characterized by automatically matching records with fields with the same name in the table, and the types of these fields with the same name can be different, so the types of fields in the table can be different, and can be applied in INNER, LEFT, etc. JOIN

例:select * from user NATURAL LEFT JOIN user2 在这里user与user2的区别是除了id之外user2字段后会加上1,这将导致连接时自动使用id进行关联,结果也确实如此	

17. The role of OPTIMIZE: optimize the defragmentation and indexing of the table

When the database stores data, the database will allocate table space and index to the data. When the data is deleted, the database will not take back these resources but wait for new data to fill these vacancies. Use the OPTIMIZE keyword to actively clear these occupied resources.

用法:optimize table 表名称	

18. RANGE partition

Based on a given interval range, the value of this field in the given interval is required to be continuous, and the corresponding partition will be used when performing the operation, which can greatly improve efficiency. The requirement is that if the table has a primary key, it is also used as a partition field Should be in the ranks of the primary key

用法:建表语句+PARTITION BY RANGE (xuehao) (PARTITION p0 VALUES LESS THAN (6),PARTITION p1 VALUES LESS THAN (11),PARTITION p2 VALUES LESS THAN (16), PARTITION p3 VALUES LESS THAN (21));

19.LEAVE Leave statement indicates to exit the flow control statement block of the specified label

It is usually used in begin...end, and loop, repeat, and while loop statements, just like break in programming, using LEAVE loop name; insert picture description here LEFT LEFT() function is a string function,
it Returns the left part of a string with the specified length.

如:SELECT LEFT(name,3) FROM user	

20. LIKE for fuzzy search

It can be used together with % and _, % represents one or more wildcards, and _ represents a character

如:SELECT * FROM user WHERE name LIKE '%i'可以查询到所有name中以i结尾的数据,将%换成_之后,查询条件就变成了查询name中以i结尾并且前面只有一个字符的数据

21. LIMIT is used for pagination query

如:SELECT * FROM user LIMIT 4,5 其中第一个参数为起始条数 如上例就是从第4条数据开始返回,第二个参数为偏移量如上例所示参数为5则表示返回从第4条的后5条数据,之前偏移量可以为-1代表查询起始条数后的所有数据,但是这个写法被mysql官方认定为一种错误,所以较新版的mysql都不能用了,最好的办法就是给一个比较大的数字

Guess you like

Origin blog.csdn.net/shugyin/article/details/124710115