1: 基础语法
# 1:判断用户登录
# 录入用户名和密码 是否登录成功
select * from employee where username='admin' and password='1234';
# 如果没有用预处理会出现 sql注入问题问题
# java获取到用户名和密码 根据用户名查询用户,再java这里进行密码的比对。
select * from employee where username='admin';
# 注意 项目中 重要用户的密码 一般都进行加密 不一定用md5,也有能用别的方式。
#2 插入语句
insert into employee (id, name, username, password)
values (1111,'阿杰','jiesen','sidansenguoda');
# mybatis save(Employee employee)
# insert into employee (id, name, username, password)
# values (#{id},#{name},#{username},#{password});
# mybatis save(@Param("employee")Employee employee)
# insert into employee (id, name, username, password)
# values (#{employee.id},#{employee.name},#{employee.username},#{employee.password})
insert into employee (id, name, username, password)
values (1111,'阿杰','jiesen','sidansenguoda');
insert into employee values (1111,'阿杰','jiesen','sidansenguoda');
# 字段一个都不能差 且顺序也不能差。
insert into employee (id, name, username, password)
values (1111,'阿杰','jiesen','sidansenguoda'),
(2222,'磊磊','alei','leileixuexue'),
(1111,'闯闯','chuang','chuanchuang');
# 3: 更新语句
update employee set password='111' where id='1111';
update employee set password='111' , name='杰哥哥' where id='1111';
#4:删除语句
delete from employee ;# 不加条件 全部删除
delete from employee where id='1111';-- 根据id删除
delete from employee where name='闯闯'; -- 根据指定条件删除
#5:基础查询
select * from dish; -- 查询所有dish数据,并展示所有字段
# 只想看dish表 name price
select name,price from dish;
2: 条件查询
# 条件查询 根据条件去数据库筛选出符合条件数据,完成展示,不符合的不展示。
/*
betweent ....and... 之间查询 [a,b] 包含a包含b
like 模糊查询
< > <= >=
in 包含只要在里面就行 not 不要 不是 非的意思
*/
# 查找 价格小于10000分且名字带 肉 的菜品数据
select * from dish where price<10000 and name like '%肉%';
# 查找 价格小于10000分 或者 名字带 肉 的菜品数据
select * from dish where price<10000 or name like '%肉%';
# 查找 名字 不带 肉的菜品数据
select * from dish where name not like '%肉%';
# name 取值 不带肉
select * from dish where not name like '%肉%';
# name带肉的 都不要。
# sql语句 弱类型语言 字符串 是引号引起来的 可以是单引 可以是双引
3: 多表查询
# 多表查询 一张表数据不够了 需要 多张表完成组合的查询
# 特点 多张表合一块 相当于形成了一张大(伪)表
# 从大表中 选取需要的字段
select * from category;
select * from dish;
# 不用关联关系的去查询 多张表 会出现 笛卡尔积
select * from category ,dish;
# 隐式内连接
select * from category,dish where category.id=dish.category_id;
# 显示内连接
select c.* from category c inner join dish d on d.category_id=c.id;
# 开发 更推荐显示内连接。
# 语法性更强。 隐式内连接追加条件 加and 显示内连接 where on 解耦 on代表关联条件 where去伪表中筛选
# 外连接
# 左外连接 右外连接
select c.* from category c left outer join dish d on d.category_id=c.id;
# 以左表为基准表,查询左表的所有数据 以及右表的关联数据。
select c.* from category c right outer join dish d on d.category_id=c.id;
# 以有表为基准表,查询右的所有数据 以及左表的关联数据。
# 子查询一种 灵活的查询方式 你查询的sql语句 可以作为另一个sql语句的语法一部分 参数值 虚拟表 in里面条件.
多表练习
SQL
自动换行开启
复制
# 查询 叫 一般检查的 检查组中 所涉及的所有的检查项信息 。要求展示 检查组基本信息和相关的检查项信息
# t_checkgroup name='一般检查'
# name为一般检查的检查组 包含什么样的检查项
# 如果 需求是 仅仅查看出来 一般检查对应的 "检查项信息"
# 可以使用子查询
select id from t_checkgroup where name='一般检查';# checkgroupId 5
select checkitem_id from t_checkgroup_checkitem where checkgroup_id=5;
# 子查询查询的
select * from t_checkitem
where id in
(
select checkitem_id from t_checkgroup_checkitem
where checkgroup_id=(
select id from t_checkgroup
where name='一般检查'
)
);
# 我们要的 检查组 及 相关的检查项信息!!
# 一张表 查询的数据不够了--再加另外的表
select tc.name 检查组名 ,tc.code 检查组编码,tcc.*,t.*
from t_checkgroup tc
join t_checkgroup_checkitem tcc on tc.id = tcc.checkgroup_id
join t_checkitem t on tcc.checkitem_id = t.id
where tc.name='一般检查'
4: 高级查询
/*
order by 排序
根据查询的结果 按照查询的某一列 进行排序
*/
select name,price,code from t_checkitem where type=2 order by code desc ;
/*
聚合函数 count() avg() sum() max() min()
分组 group by
一般来说 分组都要做聚合
*/
# 按照性别 统计检查项个数
select sex,count(*) from t_checkitem group by sex;
# 检查项里面 分 检查和检验 多少个检查 的检查项 多少个 检验的 检查项
select type,count(*)from t_checkitem group by type;