1、查询空值
–is null 空值 --is not null非空值
2、拼接concat(str1,str2)
–concat(‘连接符’,str1,str2)连接符,str1,str2严格按照顺序
–聚合函数group_concat(X,Y),其中X是要连接的字段,Y是连接时用的符号,可省略,默认为逗号。
此函数必须与GROUP BY配合使用。此题以dept_no作为分组,将每个分组中不同的emp_no用逗号连接起来(即可省略Y)。
答案:
SELECT dept_no,group_concat(emp_no) employees
FROM dept_emp GROUP BY dept_no
3、用插入来修改记录
–insert ignore into table values(); 记录存在则忽略,不存在则插入
–insert replace into table values(); 记录存在则替代,不存在则插入
4、创建表之前检查表是否存在
–create table if not exists table_name()
5、插入其他表的数据
–insert into 表1 select first_name,last_name from 表2;
6、索引
https://www.runoob.com/mysql/mysql-index.html
7、replace()替换函数:
mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便,mysql 替换函数replace()
Update table_name
SET field_name
= replace (field_name
,’from_str’,'to_str’) Where field_name
LIKE ‘%from_str%
8、rename 修改表名
–rename命令格式:rename table 原表名 to 新表名;
9、窗口函数
sum(a) over (order by b)
–https://www.nowcoder.com/practice/ae5e8273e73b4413823b676081bd355c?tpId=82&&tqId=37925&rp=1&ru=/activity/oj&qru=/ta/sql/question-ranking
count(a) over(partition by b) 分别计算不同的b所对应的count
10、date_format(),用于以不同的格式显示日期/时间数据
常用格式 对应描述
%Y --年,4 位
%m --月,数值(00-12)
%M --月名
%k --小时(0-23)
–date_format(date,’%Y-%m’) 以年月显示时间
11、length() 求长度
12、replace(“原始字符串”,“想要替换的内容”,“想要替换成的内容”)
替换字符
13、字符串截取函数
–https://blog.csdn.net/album_gyd/article/details/81365573
14、分页 limit
15、新增一列
–alter table TABLE_NAME add column NEW_COLUMN_NAME varchar(20) not null;这条语句会向已有的表中加入新的一列,这一列在表的最后一列位置。如果我们希望添加在指定的一列,可以用:
–alter table TABLE_NAME add column NEW_COLUMN_NAME varchar(20) not null after COLUMN_NAME;注意,上面这个命令的意思是说添加新列到某一列后面。
–alter table TABLE_NAME add column NEW_COLUMN_NAME varchar(20) not null first;如果想添加到第一列的话,可以用:
16、触发器
–https://www.cnblogs.com/geaozhang/p/6819648.html
17、exists关键字
–存在,exists
–不存在,not exists
select * from employees e
where not exists
(select emp_no from dept_emp d where d.emp_no = e.emp_no);
18、创建临时表
with tmp_table as (
select t1.id, t1.name, sum(t2.grade_num) as grade_sum
from user as t1 join grade_info as t2
on t1.id = t2.user_id
group by t1.id
)
select id, name, grade_sum
from tmp_table
where grade_sum = (select max(grade_sum) from tmp_table)
order by id;