增删改:
create 新建
默认条件:
primary key 主键(不重复)
auto_increment 递增
null/not null
insert into 数据库名.表格名(列1,列2……) values(数值1,数值2……):
default 自动递增,就不要具体数值,字符串不行
增加字段:
alter table 数据库名.表格名 add 列名 数据类型 默认条件
更新数据:
update 数据库名.表格名 set 值 where 条件
删除数据:
delete from 数据库名.表格名 where 条件
删除表:
drop table 数据库名.表格名
删除数据库:
drop database 数据库名
查select:
where:
between 100 and 500 表示数字区间
in(‘afd’,‘hhh’) 字符约束
like:where 字段 like……
like 'B%' B开头 '%B' B结尾
like '__b%' 第3个是b的(前面两个下划线) 如果没有%表示三个字符,最后b结尾
去重:distinct应用于所有列,而非前置他的列,除非指定的两个列都不同,否则所有行都会被检索出来(多列的组合在表中是唯一的时,该行才会被返回。如果有多行具有相同的组合,只会返回其中一行。)
限值检索范围:LIMIT 5 (1-5行) LIMIT 5,5(从开始的5行,即:5-9)
LIMIT 0,1表示第一行 LIMIT1,1就是第二行。
运算符:
“:=”是赋值 “=”是比较
执行顺序:
from确定了数据来源,where在from后进行初次数据筛选,group by对筛选后的数据进行分组,,having 对分组完成的进行筛选,order by对筛选的数据排序,然后是limit,最后select完成输出和计算
group by分组后应用聚合函数
聚合函数不能在where用:因为where在group by 之前
having可以使用聚合,一般having中也就是用聚合,其他的基本都可以放在where,使用聚合提高效率
合并:
union: 并集,并且去除重复
select union select
union all:不去重
inner join:交集 或者 join
inner join 表
on 条件
select * from egg_database.covid_month inner join egg_database.covid_total on covid_month.Country=covid_total.Country;
left join 左连接:右表符合条件的合并过来,左表不变
left join 表
on 条件
右连接同理
ifnull
ifnull(sum(a.s_score),0) 为空就返回0
mysql 50题第8题
not in
select *
from student
where s_id not in(
select
s.s_id
from
teacher t,score s,course c
where
t.t_id=c.t_id
and
c.c_id=s.c_id
and
t.t_name='张三')
not exists
select *
from student
where not exists(
select 1
from (select s.s_id
from teacher t,
score s,
course c
where t.t_id = c.t_id
and c.c_id = s.c_id
and t.t_name = '张三')x
where
x.s_id=student.s_id
)