插入数据 insert
#1. 插入完整数据(顺序插入)#语法一:
insert into 表名(字段1,字段2,字段3…字段n) values (值1,值2,值3…值n);#语法二:
insert into 表名 values (值1,值2,值3…值n);#2. 指定字段插入数据#语法:
insert into 表名(字段1,字段2,字段3…) values (值1,值2,值3…);#3. 插入多条记录#语法:
insert into 表名 values
(值1,值2,值3…值n),
(值1,值2,值3…值n),
(值1,值2,值3…值n);#4. 插入查询结果#语法:
insert into 表名(字段1,字段2,字段3…字段n) select (字段1,字段2,字段3…字段n) from 表2 where …;
View Code
删除数据 delete
#语法:
DELETE FROM 表名 WHERE CONITION;#示例:
DELETE FROM mysql.user WHERE password=’’;
View Code
更新数据 update
#语法:
UPDATE 表名 SET 字段1=值1, 字段2=值2,.. WHERE CONDITION;#示例:
update 表 set 字段名=值 where 条件
update 表 set age= 84 where name =alex;
update 表 set age= null where name =alex;
update 表 set age= 84, sex = 'female' where id = 1;
View Code
单表查询
#语法
select distinct 要查的字段 from表
where 条件
group by 分组
having 过滤
order by 排序
limit 取前n个#执行顺序
from --> where --> group by --> having --> select --> distinct --> order by -->limit1.找到表:from
2.拿着where指定的约束条件,去文件/表中取出一条条记录3.将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组4.将分组的结果进行having过滤5.执行 select6.去重 distinct7.将结果按条件排序:order by8.限制结果的显示条数#二 \ 简单查询#查询所有的字段\单个字段\给字段重命名\给字段去重
select * from表;
select 字段名1,字段名2from表;
select distinct 字段名1from表;
select 字段名 as 新的临时名字from表;#查询数据的四则运算
select emp_name,salary*12 from表;
select emp_name,salary*12 as annua_salary fromemployee;#定义显示格式 concat/concat_ws
select emp_name,salary fromemployee;#姓名 : alex, 薪资:100000
select concat('姓名 :',emp_name,', 薪资 :',salary) fromemployee;
select concat('姓名 :',emp_name),concat('薪资 :',salary) fromemployee;#姓名 :薪资#alex : 100000
select concat_ws(':',emp_name,salary) fromemployee;
select concat_ws(':',emp_name,salary) as annual_salary fromemployee;#case语句
SELECT(#if条件判断
CASE #一个if条件判断句的开始
WHEN emp_name = 'jingliyang' #if
THEN emp_name #then if条件成立之后做的事儿
WHEN emp_name = 'alex' ## elif 另一个条件
THEN CONCAT(emp_name,'_BIGSB') # ELSE #else
concat(emp_name, 'SB') #没有then 直接就是上述条件不满足都走这个分支
END #end 就表示这个case语句结束了
) as new_name
FROM employee;
View Code
where 语句中可以使用:
1. 比较运算符:> < >= <= <> !=
2. between 80 and 100
3. in(80,90,100)
4. like 'egon%'
pattern可以是%或_,
%表示任意多字符
_表示一个字符
5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
# 1:单条件查询
SELECT emp_name FROM employee WHERE post='sale';
# 2:多条件查询
SELECT emp_name,salary FROM employee WHERE post='teacher' AND salary>10000;
# 3:关键字 BETWEEN AND
SELECT emp_name,salary FROM employee WHERE salary BETWEEN 10000 AND 20000;
SELECT emp_name,salary FROM employee WHERE salary NOT BETWEEN 10000 AND 20000;
# 4:关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS)
SELECT emp_name,post_comment FROM employee WHERE post_comment IS NULL;
SELECT emp_name,post_comment FROM employee WHERE post_comment IS NOT NULL;
SELECT emp_name,post_comment FROM employee WHERE post_comment=''; # 注意''是空字符串,不是null
# 5:关键字IN集合查询
SELECT emp_name,salary FROM employee WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ;
SELECT emp_name,salary FROM employee WHERE salary IN (3000,3500,4000,9000) ;
SELECT emp_name,salary FROM employee WHERE salary NOT IN (3000,3500,4000,9000) ;
# 6:关键字LIKE模糊查询
# 通配符’%’
SELECT * FROM employee WHERE emp_name LIKE 'eg%';
# 通配符’_’
SELECT * FROM employee WHERE emp_name LIKE 'al__';
group by:
#单独使用GROUP BY关键字分组
SELECT post FROM employee GROUP BY post;#注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数
#GROUP BY关键字和GROUP_CONCAT()函数一起使用
SELECT post,GROUP_CONCAT(emp_name) FROM employee GROUP BY post; #按照岗位分组,并查看组内成员名
SELECT post,GROUP_CONCAT(emp_name) as emp_members FROM employee GROUP BY post;#GROUP BY与聚合函数一起使用
select post,count(id) as count from employee group by post; #按照岗位分组,并查看每个组有多少人
#常用聚合函数
SELECT COUNT(*) FROM employee; #计数
SELECT MAX(salary) FROM employee; #最大
SELECT MIN(salary) FROM employee; #最小
SELECT AVG(salary) FROM employee; #平均
SELECT SUM(salary) FROM employee WHERE depart_id=3; #求和
View Code
HAVING过滤:
HAVING与WHERE不一样的地方在于!!!!!!
#!!!执行优先级从高到低:where > group by > having
#1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。
#2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数
使用正则表达式查询:
SELECT * FROM employee WHERE emp_name REGEXP '^ale';
SELECT * FROM employee WHERE emp_name REGEXP 'on$';
SELECT * FROM employee WHERE emp_name REGEXP 'm{2}';