zoukankan      html  css  js  c++  java
  • 转 父表字表统计查询的sql练习

    create table father(
           f_id number(2) primary key,
           f_name varchar2(10)
    );
    create table son(
           s_id number(2) primary key,
           s_name varchar2(10),
           s_height number(3,2),
           s_money number,
           f_id number(2),
           foreign key(f_id) references father(f_id)
    );
    --插入父亲信息
    insert into father values(1,'何胜达');
    insert into father values(2,'何忠达');
    insert into father values(3,'何国达');
    insert into father values(4,'陌生人');
    --插入儿子信息
    create sequence s1;
    insert into son values(s1.nextval,'何亮',1.70,6000,1);
    insert into son values(s1.nextval,'何星',1.68,4000,1);
    insert into son values(s1.nextval,'何正安',1.73,7000,2);
    insert into son values(s1.nextval,'何正明',1.72,4000,2);
    insert into son values(s1.nextval,'何正元',1.68,8500,2);
    insert into son values(s1.nextval,'何正陆',1.66,5000,3);
    insert into son(s_id,f_id)values(s1.nextval,4);
    --查询s_id、s_name、f_id
           select s_id,s_name,f_id from son;
    --查询各儿子涨价20%以后的新学费,注意,8000块以下的不涨价。
           select s_name,s_money*1.2 from son where s_money>8000;
    --查询s_id、s_name、f_id、f_name
           select f.*,s.s_id,s.s_name
           from father f
           join son s
           on f.f_id=s.f_id;
    --查询f_id、f_name、儿子数(没有儿子的不显示)
           select f.f_id,f.f_name,count(s.s_name)as 有几个儿子
           from father f
           join son s
           on f.f_id=s.f_id
           group by f.f_id,f.f_name
           having count(s.s_name)>0;
    --查询f_id、f_name、儿子数(没有儿子的个数显示为0)
           select f.f_id,f.f_name,count(s.s_name)as 有几个儿子
           from father f
           join son s
           on f.f_id=s.f_id
           group by f.f_id,f.f_name
           having count(s.s_name) is not null;
    --找出不止有1个儿子的father信息:f_id、f_name、儿子数
           select f.f_id,f.f_name,count(s.s_name) as 不止一个儿子
           from father f
           join son s
           on f.f_id=s.f_id
           group by f.f_id,f.f_name
           having count(s.s_name)>1;
    --找出儿子最多的father信息:fid、fname、儿子数
           select f.f_id,f.f_name,count(s.s_id) as 个数
           from father f
           join son s
           on f.f_id=s.f_id
           group by f.f_id,f.f_name;
    --、找出fid为(7,9,11)的father中,各儿子的身高
         select s_name,s_height from son where f_id in(1,2,3);
    --找出所有father中身高最高的儿子。
         select s_name from son where s_height=(
                  select max(s_height) from son
           );
    --找出各father中身高最高的儿子 
           select son.* from son,
                  (select f_id,max(s_height) 最高儿子 from son group by f_id) x
            where son.f_id=x.f_id and son.s_height=x.最高儿子;
    --找出身高在1.8到1.65之间的所有儿子及父亲信息:fid,fname,sid,sname,height;
    --这里用到了where,之所以能用是因为前面没有用到聚合函数
          select f.*,s.s_id,s.s_name,s.s_height
          from father f
          join son s
          on f.f_id=s.f_id
          where s.s_height between 1.65 and 1.8;
    select * from father;
    select * from son;

    <!DOCTYPE root [
     <!ELEMENT root (父亲+,儿子*)>
     <!ELEMENT 父亲 EMPTY>
     <!ELEMENT 儿子 EMPTY>
     <!ATTLIST 父亲
      fid CDATA #REQUIRED
      姓名 CDATA #REQUIRED
     >
     <!ATTLIST 儿子
      sid CDATA #REQUIRED
      姓名 CDATA #REQUIRED
      性别 (男|女) #REQUIRED
      年龄 CDATA #REQUIRED
      学费 CDATA #REQUIRED
      身高 CDATA #IMPLIED
      fid IDREFS #REQUIRED
     >
    ]>
    <root>
     <父亲 fid="P_1" 姓名="何胜达"/>
     <父亲 fid="P_2" 姓名="何中达"/>
     <父亲 fid="P_3" 姓名="何国达"/>
     <父亲 fid="P_4" 姓名="陌生人"/>
     <儿子 sid="1" 姓名="何亮" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_1"/>
     <儿子 sid="1" 姓名="何星" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_1"/>
     <儿子 sid="1" 姓名="何正安" 性别="男" 年龄="24" 学费="1000" 身高="1.72" fid="P_2"/>
     <儿子 sid="1" 姓名="何明" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_2"/>
     <儿子 sid="1" 姓名="何元" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_2"/>
     <儿子 sid="1" 姓名="何陆" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_3"/>
    </root>

  • 相关阅读:
    prototype.js超强的javascript类库
    MySQL Server Architecture
    Know more about RBA redo block address
    MySQL无处不在
    利用Oracle Enterprise Manager Cloud Control 12c创建DataGuard Standby
    LAMP Stack
    9i中DG remote archive可能导致Primary Database挂起
    Oracle数据库升级与补丁
    Oracle为何会发生归档日志archivelog大小远小于联机重做日志online redo log size的情况?
    Oracle Ksplice如何工作?How does Ksplice work?
  • 原文地址:https://www.cnblogs.com/LYL-1314/p/4245102.html
Copyright © 2011-2022 走看看