Some grammatical specifications: it is
not case sensitive, it is recommended that keywords be capitalized, and other lowercase.
Each command ends with a semicolon
if it is required. It can be indented or line
- wrapped. Single-line comment:
#Comment text
– comment text
Multi-line comment
/* comment text*/
The basic statement
shows all databases :show databases;
Open the specified library :use 库名;
View all tables in the current library :show tables;
View all tables of other libraries :show tables from 库名;
Create table :
create table 表明{
列名 列类型,
列名 列类型 (最后一个就不用加逗号了)
};
View the structure of the table :desc 表名;
View server version : select version();
or cmd
undermysql -V / mysql --version
Basic query:
select 查询列表 (查询列表可以是:表中的字段,常量值,表达式,函数)
from 表名
The result of the query is a virtual table
Query a single field in the table :select 需要查询的字段名 from 表名; (注意最后的分号)
Multiple fields in the query table :
select 查询的字段1,查询的字段2,查询的字段3 from 表名; (字段名中间用逗号隔开,顺序可以和原始表不一致)
All fields in the query table :
method one: select 点击表名(或者手动输入全部的,中间用逗号隔开,F12可以自动调整格式)
method two:select * from 表名(但是要注意使用* 的时候顺序是和原始表的顺序是一致的)
Note: In
MYsql, it is directly used to view equal values =
, not ==
.
` This symbol is not a single quotation mark, but an accent mark. My keyboard is under the Esc keyboard.
Not repeat keywords need to distinguish between when you can add emphasis
Query constant value
select 100 ;(100是可以替换的)
select ‘Tom’ ; (用的是单引号,且SQL认为非数值的都是字符)
Query expression :select 100*98 ;
Query function :select version();
Create an alias:
Method 1: select 100%98 as 结果; (使用as关键字)
Advantages: Easy to understand
If the field to be queried has the same name, it can be distinguished by using an alias.
Method 2: select 100%98 as 结果 (省略as关键字,使用空格)
If the alias contains keywords or spaces, you need to add double quotation marks, or single quotation marks. Double quotes are recommended.
Deduplication :select distinct department_id from table; (需要加上distinct关键字)
The function of the plus sign+ :
Case: Query the employee's name and the employee's last name, and display them as names
select last_name+first_name as 姓名 from employees;(这个写法是错误的)
select ‘123’+90
; This sentence is an attempt to convert a character type to a numeric type. If the conversion is successful, the addition operation will continue. This sentence can be converted successfully. The characters that failed to be converted are converted to 0;
As long as one party is Null, the result is Null
In MySQL, the function of + is only to add values
How to stitch : use functionsconcat(str1,str2, ...);
ifnull(表达式1,表达式2);
Expression 1 puts a name that may be empty, and expression 2 is the value you want to return if it is empty.
Condition query:
select *
from table
where 筛选条件;
( Order of execution : first execute from table, first check whether there is this table, then execute where, and finally select)
Category :
Filter by conditional expression:
Conditional operator : >, < , =, <>(!=), >=, <=,
Query by logical expression:
Logical operator :and, or, not(&&, ||, !),
Fuzzy query like, between and, in, is null
Example of like query:
Find the name that contains the letter a in the name:
select * from table where name like ‘%a%’
(Note two points, the character should be in single quotes, because a can be in the middle or behind, so add% to indicate a wildcard
Wildcard:
%
any number of characters (including 0 characters)
_
represents any single character
If you need to find a name that contains an underscore:
add an escape character before it: \
but it is recommended $ ;escape ‘$’;
($ can be arbitrarily specified)
between and
Is a critical value
in:
Determine whether a certain value belongs to a certain item in the in list.
Advantages of using in: improve sentence conciseness;
the types of values in the in list must be unified or compatible (underscore or% is not supported)
is null
Contrary to is not null
=
or <>
not used to determine a null value
, but can be written as<=> null
Security equal <=>
readability is poor, not very recommended
SQL sort query:
order by 排序列表 asc(升序:从低到高) | desc(降序:从高到低)
The default is ascending
The function length()
can be sorted according to the function, the length can be found
Sort by multiple fields :order by 字段1,desc, 字段2,asc;
order by
Words and sentences are generally last, limit
except for clauses
Order of execution:from table , where , select, order by
Function :
Method to call:select 函数名(实参列表) from 表
One-line function :concat., ;length, ifnull
Grouping function: used
for statistics,
also known as statistical function, aggregate function, group function
Character function:
length
utf8 The next Chinese character occupies three bytes.
concat
Connect
upper
to uppercase
lower
and lowercase
substr/substring
(note that the index starts from 1).
substr(“TomAndBob”,5);
This sentence means to output the string after index 5.
substr(“TomAndBob”,2,5);
This sentence means to output from the index The 5 characters after 2 are
instr(str1,str2)
used to return the starting index of str2 in str1. If it is not found, 0 is
trin()
used to remove the specified letters before and after it, if not specified, the space is removed.
Example: trin(‘a’ from ‘aaajkjjjsjjsjjsssaaaajjjjjjjjaaaaaaaaaaaa’);
(Note that these are the specified characters before and after the removal, not including the middle)
lpad
(Note that the letter L, not i)
lpad(‘str1’,10,’*’);
Fill the specified characters on the left, and the final length is consistent with the specified length, although the specified length may be longer than str1 should be small.
rpad:
Right fill
replace:
replacement: replace(str1, str2, str3), replace str2 in the string str1 with str3;
Mathematical Functions:
round()
rounded
Example: round (1.65) as a result of 2, round (1.65,1) holding the two after the decimal point is
ceil()
rounded up
floor()
rounded down
truncate()
truncated
example: turncate(1.69,1)
means to one decimal
mod()
modulo
Date function:
now()
return the current date and time of the system
curdate()
return the current date, excluding the time
curtime()
return the current time, excluding the date
select year(now())
You can only get in
select year(‘1998-1-5’)
can get in only
if you need to display is the English name, you can use year_name()
to achieve
Flow control function:,
if(expr1, expr2, expr3)
if expr1
true, the value is expr2
, otherwise expr3
(similar to the ternary operator)
case:
Usage 1: case
Add the variable or expression that needs to be judged
when 常量1 then 要显示的值1或语句1;
when 常量2 then 要显示的值2或语句2;
...
else 要显示的值n或语句n;
end
Usage 2:case
when 条件1 then 要显示的值
when 条件2 then 要显示的值
...
else 要显示的值或者语句
end