09-12:解题&查询效率&索引

2018-09-12  本文已影响0人  神坛下的我

解题09-11

连接查询:
select d.name from department d join 
  (select e.did,e.name join employee e join es 
    on e.eid=es.eid 
    where es.sid in( select sid from skill 
    where substr(descript,1,4)='驾驶小车' or substr(descript,1,4)='驾驶货车')
    j where d.did = j.did
)
相关子查询:
select dp.name from department dp 
where exists(
    select * from employee e
    where eid in(
    select eid from es 
    where sid in(
        select sid from skill
        where substr(descript,1,4)='驾驶小车' or substr(descript,1,4)='驾驶货车'
    ) )and e.did=dp.did
)
子查询:
    select * from employee e1
    where e1.did in 
    (
    select e.did from employee e 
    join es
    on e.eid = es.eid 
    join skill s
    on es.sid=s.sid and s.descript='外语'
    )
相关子查询:
    select * from employee e1 
    where exists(
    select e2.did from employee e2 
    join es
    on e2.eid = es.eid 
    join skill s
    on es.sid=s.sid and s.descript='外语' 
    where e1.did=e2.did
    )
select * from student s1 
where exists(
    select * from class 
    where s1.cno in (
    select * from 
    (select cno as countno from student s2 
    where avgscore>=80 group by cno 
    having count(*) >= 2) t )
)
逻辑有点问题:
SELECT * FROM employee e 
WHERE EXISTS(
    SELECT t.did FROM (
    SELECT e.eid,e.name,es.sid,e.did FROM employee e
    JOIN es ON es.eid = e.eid 
    GROUP BY e.eid,e.name 
    HAVING COUNT(es.sid)>=2
    ) t
    WHERE t.did IN(
    SELECT did FROM employee 
    GROUP BY did 
    HAVING COUNT(eid)>2)
    AND e.did=t.did)
参考1,看本部门是否在同时满足条件一和条件二的部门列表中:
select * from employee e 
where exists(
    SELECT t.did FROM (
        -- 查询出了哪些员工掌握了2门以上的技能*
        SELECT e.eid,e.name,es.sid,e.did FROM employee e
        JOIN es ON es.eid = e.eid 
        GROUP BY e.eid,e.name 
        HAVING COUNT(es.sid)>=2 ) t 
        -- 当前员工和掌握2门以上技能的员工是不是同一部门
        where t.did=e.did 
        group by t.did 
        having count(t.did) >=2
)
参考2,先看本部门是否满足条件一,再看是否满足条件二:
SELECT * FROM employee e
 WHERE EXISTS(
  SELECT * FROM department 
  WHERE e.did IN( -- 我的部门在不在符合要求的部门之中
   -- *** 找出那些员工掌握了2门以上的技能
   SELECT t.did FROM (
         SELECT e.eid,e.name,es.sid,e.did FROM employee e
         JOIN es ON es.eid=e.eid
         GROUP BY e.eid,e.name
         HAVING COUNT(es.sid)>=2) t
    -- ***
         GROUP BY t.did
         HAVING COUNT(t.did) >= 2)  -- 只留下有2人以上符合要求的部门
 )

查询效率与索引

explain select s.sno,s.name from teacher t join student s 
on t.area=s.area 
where t.tname='陈涛'

explain select s.sno,s.name from teacher t join student s 
on t.area=s.area and t.tname='陈涛'

explain select s.sno,s.name from student s 
where area = (
    select area from teacher 
    where tname='陈涛'
)
create index idx_student_sno_name_area on student(sno,name,area)
create index idx_teacher_tno_tname_area on teacher(tno,tname,area)
drop index idx_teacher_tno_tname_area on teacher
-- 删除索引sqlserver写法
drop index teacher.idx_teacher_tno_tname_area
-- 从其他表复制一次数据
insert into teststudent select * from student
-- 再重复翻倍复制自身数据
insert into teststudent select * from teststudent
-- 比delete执行快,但其不记录日志,找不回数据
truncate table teststudent
select * into testconsumer from Consumer
  1. 构建没有索引的表
  2. 复制数据
  3. 没有索引执行连接
  4. 创建索引执行连接

新题

解法一:
select distinct sc1.exampoint from sc sc1 
where exists(
    select score from sc sc2 
    where sc2.score<60 and sc1.exampoint=sc2.exampoint 
)
解法二:
SELECT DISTINCT sc1.`exampoint`,

  (SELECT COUNT(*) AS 'lessno' FROM sc sc2
   WHERE sc1.exampoint = sc2.exampoint AND score > 60)
   /
  (SELECT COUNT(*) AS 'countno' FROM sc sc2
  WHERE sc1.exampoint = sc2.exampoint) AS '通过率'
  
  
  ,exampoint 
  FROM sc sc1
WHERE 
  (SELECT COUNT(*) AS 'lessno' FROM sc sc2
  WHERE sc1.exampoint = sc2.exampoint AND score > 60) 
   /
  (SELECT COUNT(*) AS 'countno' FROM sc sc2
  WHERE sc1.exampoint = sc2.exampoint)  < 1
select distinct ad.adno from admin ad 
where exists(
    select score from sc 
    where sc.score<60 and ad.adno = substr(sc.adno,4,1)
)
上一篇下一篇

猜你喜欢

热点阅读