赞
踩
- --创建数据库
- create database StudentMS
-
- --使用数据库
- use StudentMS
-
- --删除数据库
- --drop database StudentMS
- --创建学生表 (属性:姓名、学号(pk)、学院、出生日期、性别、籍贯)
- create table xs
- (
- name varchar(10) not null,
- id varchar(10) not null,
- xy varchar(10),
- birthday datetime,
- xb char(2),
- jg varchar(8)
- )
-
- --创建学生表主键:学号
- alter table xs
- add constraint
- pk_xs primary key(id)
-
-
- --创建表学生表外键:系代号 此表中xdh已被省略
- alter table xs
- add constraint
- fk_xs foreign key(xdh)
- references xb (xdh)
- insert into xs
- (id, name, xb, birthday, xy, jg)
- values('1160001', '刘备', '男', '1991-11-5', '软件学院', '河北省');
总共插入10个学生的数据,其中如birthday可为null,如下:
- select id as 学号, name as 姓名, year(getdate())-year(birthday) as 年龄, birthday as 出生日期
- from xs;
-
- select id as 学号, name as 姓名, datediff(YY,birthday,getdate()) as 年龄, birthday as 出生日期
- from xs;
输出如下所示,后面也可以计算不同年龄段的人数:
- --方法1:group by 列分组
- select xy from xs group by xy;
-
- --方法2:列出不同的值
- select distinct xy from xs;
输出结果:
- --匹配姓名以"黄"开头的学生
- select * from xs where name like '黄%';
-
- --匹配学院包含"计算机"的学生
- select * from xs where xy like '%计算机%';
-
- --匹配姓名以"尚香"结尾的学生
- select * from xs where name like '%尚香';
输出结果:
- select T_WSTB_DLPYJBQKB.DL_BHXNZYMC, ZY_NAME
- from T_WSTB_ZYJBQK, T_WSTB_DLPYJBQKB
- where T_WSTB_DLPYJBQKB.DL_BHXNZYMC like '%' || ZY_NAME ||'%'
输出如下所示,也可以某个字段包含的个数:
select * from xs where jg in ('河北省','河南省');
输出结果:
select xy as 学院名称 from xs where xy='软件学院' group by xy;
输出结果:
- --子查询统计人数
- select a.a_num as 软院人数, b.b_num as 计院人数, c.c_num as 自动化人数,
- d.d_num as 男生人数, e.e_num as 女生人数, f.f_num as 河北河南人数
- from
- (select count(*) as a_num from xs where xy='软件学院') a,
- (select count(*) as b_num from xs where xy='计算机学院') b,
- (select count(*) as c_num from xs where xy='自动化学院') c,
- (select count(*) as d_num from xs where xb='男') d,
- (select count(*) as e_num from xs where xb='女') e,
- (select count(*) as f_num from xs where jg in ('河北省','河南省')) f;
输出结果:
- --创建学院表
- create table table_xy
- (
- name varchar(10) not null,
- id varchar(10) not null
- );
-
- --插入数据
- insert into table_xy(id, name) values('001', '软件学院');
- insert into table_xy(id, name) values('002', '计算机学院');
- insert into table_xy(id, name) values('003', '自动化学院');
- insert into table_xy(id, name) values('004', '法学院');
输出如下,这里插入一个法学院,它的统计结果都为空:
- select distinct name as 学院名称,
- (select count(*) from xs where xs.xy=table_xy.name) as 总人数,
- (select count(*) from xs where xs.xy=table_xy.name and xs.xb='男') as 男生总数,
- (select count(*) from xs where xs.xy=table_xy.name and datediff(YY,birthday,getdate())<=25) as 二十五岁人数,
- (select count(*) from xs where xs.xy=table_xy.name and xs.jg in ('河北省','河南省')) as 河北河南生源地
- from table_xy;
输出结果:
- select t1.ZFJGSL as 数量,
- trunc( 1.0 * (select ITEM_VALUE from T_WSTB_YJBKBYSJYQK
- where RECORD_YEAR=(to_char(sysdate, 'yyyy')-2) and FIRST_NUM='2' and SECOND_NUM='2') /
- (select ITEM_VALUE from T_WSTB_YJBKBYSJYQK
- where RECORD_YEAR=(to_char(sysdate, 'yyyy')-2) and FIRST_NUM='2' and SECOND_NUM='1') * 100,
- 2) as 比例
- from
- (select ITEM_VALUE as ZFJGSL from T_WSTB_YJBKBYSJYQK
- where RECORD_YEAR=(to_char(sysdate, 'yyyy')-2) and FIRST_NUM='2' and SECOND_NUM='2') t1;
输出如下所示:
- select DL_NAME as num1,
- length(DL_BHXNZYMC)-length(replace(DL_BHXNZYMC,',',''))+1 as num2
- from T_WSTB_DLPYJBQKB
运行结果如下所示:Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。