创建表:
CREATE TABLE teacher
(
id
CHAR(5) PRIMARY KEY,
name
varchar(20) NOT NULL,
gender
enum('man','women') NOT NULL,
poisition
enum('助教‘,'讲师','客服'
);
插入
INSERT INTO(表民)VALUES(内容)
普通插入
INSERT INTO `teachers` VALUES('12','12','MAN','讲师');
指定插入
insert into `teachers` ('id','gender','name') values('0511','man','hello')
插入多行数据
insert into `teachers`
values ('111','nihao','man'),
('222','www','women'),
('333','nnn','man');
修改
update `teachers` set `position` = '讲师’ where `id`= '05110'
查询
select * from `teachers` where 1
* 代表列
where 后面代表行
select 'name','gender' from `teachers` where 1;
指定行查询
select `name` from `teachers` where `position`= '讲师';
select `name` from `teachers` where `position`= '讲师' or `position` = '教授';
select `name` from `teachers` where (`position`= '讲师' or `position` = '教授') and `gender` = `man`;
优先级问题
排序:
select `name` from `teachers` where (`position`= '讲师' or `position` = '教授') and `gender` = `man` order by `id` desc(降序)
多条件排序:
select `name` from `teachers` where (`position`= '讲师' or `position` = '教授') and `gender` = `man` order by `id` desc,name;
分组查询:
GROUP BY 通常不和order by 一起使用,group by通常是配合聚合函数一起使用。
select `name` from `teachers` where 1 group by `gender`
select count(*) from `teachers` where 1 group by `gender`; 计数
select `gender`,count(*) from `teachers` where 1 group by `gender`
别名
select `gender` as `GENDER`,count(*) as `num` from `teachers` where 1 group by `gender`
删除
delete from `teacher` where 1 全删!!跑路吧。。
delete from `teacher` where `name` = 'caoliang'
在删除之前一定要确认where ,同时,需要在相同的where条件先select一次。!!
连接:可以解决80%的问题,还有一种是子查询
1.内连接
第一张表的每一行,和第二张表的每一列都连接一次。
select a.id,a.name from 'class' as a
inner join 'student' as b
on a.id= b.id
where b.id=9
2.外连接
主要有左连接和右连接,在写语句时,写在前面的表叫左表,后面的叫右表。left join 是保持左边不变,right是保持右边不变。
select a.id,a.name from 'class' as a
left join 'student' as b
on a.id= b.id