mysql 查询


-- 多表查询

select * from tb_dept,tb_emp;

内来链接

-- ===================内连接 ========================
-- A 查询员工的姓名 , 及所属的部门名称 (隐式内连接实现)
   select tb_emp.name,tb_dept.name from  tb_emp,tb_dept where tb_emp.id=tb_emp.id;

-- 推荐使用
    select a.name,b.name from  tb_emp a, tb_dept b  where a.dept_id= b.id;

-- B 查询员工的姓名 , 及所属的部门名称 (显示内连接实现)
select  tb_emp.name ,tb_dept.name from tb_emp inner join tb_dept on tb_emp.id=tb_dept.id;

 外链接

-- =====================外连接=============
-- A  查询 员工的所有 员工的姓名  和对应的部门名称 (左外链接)
select r.name,t.name  from  tb_emp r left join tb_dept t on r.dept_id=t.id;

-- B  查询 部门的所有名称 和对应的员工姓名 (右外链接)
 select e.name,d.name from  tb_emp e right join tb_dept d on e.dept_id=d.id;

 子查询

-- =================== 子查询====================
  -- 表量子查询
 -- A 查询 "教研部" 的 所有员工信息
 -- a 查询 "教研部"的部门的ID-tb_dept
select  id from tb_dept  where name ='教研部';
 -- b  查询在 "房东白" 入职之后的员工 信息- tb_emp
select * from tb_emp r where r.dept_id =2;
select * from tb_emp r where r.dept_id =(select  id from tb_dept  where name ='教研部');

-- B  查询在 "房东白" 入职之后的员工 信息
-- a 查询 房东白的入职时间
select entrydate from  tb_emp r  where  r.name='方东白';

-- b 查询在 "房东白" 入职之后的员工 信息
select  * from tb_emp f where f.entrydate>(select entrydate from  tb_emp r  where  r.name='方东白');

例子查询


-- ==============子查询 =====================
-- A 查询 "教研部" 和 '咨询部' 的所有员工信息
select id from tb_dept  where name='教研部'or name='咨询部';
 -- 根据ID查询 该部门下的员工信息
 select * from tb_emp v where v.dept_id=3 or v.dept_id=2;
 select * from tb_emp v where v.dept_id in (3,2);
-- 完整2   A 查询 "教研部" 和 '咨询部' 的所有员工信息
 select * from tb_emp v where v.dept_id in (select id from tb_dept  where name='教研部'or name='咨询部');

行子查询

-- ================行子查询 ===============
-- A 查询与 “韦一笑” 的入职日期 及 职位都相同的员工信息
-- a 先找出韦一笑 职位 入职时间
select   name, job, entrydate from  tb_emp g where g.name='韦一笑';
select  entrydate, job from  tb_emp g where g.name='韦一笑';

select * from tb_emp j where  j.job=2 and j.entrydate='2007-01-01';
select * from tb_emp j where j.entrydate='2007-01-01' and  j.job=2;
select * from tb_emp  where  (entrydate,job)=('2007-01-01',2);
select * from tb_emp  where  (entrydate,job)=(select  entrydate, job from  tb_emp g where g.name='韦一笑');

表子查询

-- =================表子查询============
-- A 查询入职时间 是 “ 2006-01-01” 之后 的员工信息 及其部门的名称·
    select * from tb_emp where  entrydate>'2006-01-10';
-- a 查询这部分员工信息及其部门名称 -
   select  e.* , d.name from  (select * from tb_emp where entrydate > '2005-01-10') e,tb_dept d where e.dept_id = d.id;

-- 1 查询价格低于 10元 的菜品的名称 、价格 及其 菜品的分类名称
select d.name,d.price, c.name from dish d, category c where d.price<10 and d.category_id= c.id;
 -- 2.查询所有价格在 10元(含)到50元(含)之间 且 状态为”起售”的菜品名称、价格及其分类名称
select d.name,d.price, c.name  from dish d  left join category c on d.category_id= c.id where d.price between 10 and 50 and d.status=1;
 -- 3 查询每个分类下最贵的菜品,展示出分类的名称、最贵的菜品的价格
 select c.name, max(d.price) from  dish d ,category c  where d.category_id =c.id group by  c.name;
 -- 4 查询各个分类下 菜品状态为“起售”,并且 该分类下菜品总数量大于等于3 的 分类名称
 select c.name ,count(*) from dish d ,category c where d.category_id=c.id and d.status=1 group by c.name having count(*)>3;

 -- 5.查询出“商务套餐A”中包含了哪些菜品 (展示出套餐名称、价格,包含的菜品名称、价格、份数
 select  s.name,s.price,d.name,d.price ,sd.copies from  setmeal s ,setmeal_dish sd, dish d where
  s.id=sd.setmeal_id and sd.setmeal_id=d.id and s.name='商务套餐A';
 -- 6查询出低于菜品平均价格的菜品信息(展示出菜品名称、菜品价格)
 select avg(price) from  dish  ;
select * from dish where price<37.736842;
select * from dish where price<(select avg(price) from  dish );

事务

 


-- =================开启事务========================
-- 1 开启
start transaction ;
 -- 删除部门
 delete  from  tb_dept where id =3;
 -- 删除部门下的用户
 delete from  tb_dept where  id==3;
 -- 提交事务
 commit ;

 -- 操作不完整退回操作
 --  回滚事务
 rollback ;

 

 

数据库索引

 

-- ===============================索引============================


-- 创建 索引
create  index  myjava on tb_sku(id)

结构 

索引语法

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值