当前位置:首页 » 数据仓库 » 图书馆数据库多表查询sql语句
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

图书馆数据库多表查询sql语句

发布时间: 2022-10-23 01:25:33

sql数据库图书馆求助,如何用select实现如下查询

1:select ((select count(*) from borrow)/(select count(*) from cards)) as AverageNumber
2:等高手了
3:select title from books where bno not in(select distinct(bno) from borrow) or bno in(select bno from borrow where datediff(year,borrow_date,getdate())>=2)
4:with t as (select bno,count(*) as cou from borrow group by bno)select bno from t where cou in(select max(cou) from t)

看似简单的 其实蛮费劲的。。。

② 3.21考虑图3-21中的图书馆数据库。用SQL写出如下查询:

select name from member,book,borrowed where member.memb_no=borrowed.memb_no and book.isbn=borrowed.isbn and borrowed.publisher="xxx"


select name from member where memb_no in  (select memb_no from borrowed group by memb_no having count(*) = (select count(*) from book where publisher="xxx"))

select name,publisher from member,borrowed,book group by name,publisher where member.memb_no=borrowed.memb_no
and
book.isbn=borrowed.isbn
having
count(*)>5

select(select count(*) from borrowed)/ (select count(*)from member)

我没有数据, 所以凭空写的, 思路差不多这样吧。有问题私信我吧. 你的列有关键字name,如果是mysql的话加``,sqlserver加[]。

③ 有关图书馆借阅系统的SQL语句

修改列名sp_rename 'mytable.name','新的列名'
但是次功能会破坏数据库结构请慎用.
exec sp_rename ‘table.oldfield’,’newfield’,’column’

Select的基本查询
1. 选择所有列
Select *from student
2. 选择指定的若干列
Select s_avgrade , s_name
From student
3. 构造计算列
Select s_name 姓名,year(getdate())-year(s_birthday) 年龄
From student;
带DISTINCT的查询
SELECT DISTINCT s_dept 所在的系
FROM student;
比较上下的区别
select s_dept 所在的系
from student;
带WHERE子句的条件查询
select s_no 学号,s_name姓名,s_sex性别,s_avgrade平均成绩
from student
where s_avgrade>=90;
带BETWEEN的范围查询
select s_name 姓名,s_sex 性别,s_dept 系别,s_avgrade 平均成绩,s_brithday 出生年月
from student
where s_brithday ("not between") BETWEEN '1986-00-00' and '1987-00-00';(这里的日期从 char 数据类型到 smalldatetime 数据类型的转换导致 smalldatetime 值越界,全部写0好像不对,具体一点到是可以运行)
-----------------------------带IN的范围查询
select s_no 学号,s_name 姓名,s_sex 性别,s_speciality 专业,s_dept 系别,s_avgrade 平均成绩,s_brithday 出生年月
from student
where s_speciality in('电子信息工程','生物工程');(查询所选定s_speciality的student的信息)

select s_no 学号,s_name 姓名,s_sex 性别,s_speciality 专业,s_dept 系别,s_avgrade 平均成绩,s_brithday 出生年月
from student
where s_speciality='电子信息工程'or s_speciality='生物工程');

-------------------带GROUP的分组查询
1.select s_dept 系别,count(*) 人数
from student
group by s_dept;(统计各个s_dept的人数)

2.select s_dept 系别,count(*) 人数
from student
group by s_dept
having count(*)>=3; (统计各个s_dept的人数,但是只输出人数大于3的)
带like的匹配查询和带is的空值查询
--------------------------带like的匹配查询
select s_no 学号,s_name 姓名,s_sex 性别,s_speciality 专业,s_dept 系别,s_avgrade 平均成绩,s_brithday 出生年月
from student
where s_name like 'y%';(只能查开头为y的名字且为第一个字,如果要具体查出"yd",就应写"y%d%") )
空值null的查询
Select *
from student
where s_avgrade=null;——错误
Select *
from student
where s_avgrade is null;——正确
------------使用Order排序查询结果
如果ASC和DESC都没有选择,则按依据列进行升序排列,即ASC为默认值
1.select s_no 学号,s_name 姓名,s_sex 性别,s_speciality 专业,s_dept 系别,s_avgrade 平均成绩,s_brithday 出生年月
from student
where s_sex='男'
order by s_avgrade DESC;
2.select s_no 学号,s_name 姓名,s_sex 性别,s_speciality 专业,s_dept 系别,s_avgrade 平均成绩,s_brithday 出生年月
from student
where s_sex='男'
order by s_avgrade DESC,s_no;(先按s_avgrade的要求(DESC)排序,之后按s_no(默认的是升序排列),如果后面仍然有要求,依前面类推)

-------------连接查询
同时涉及两个或两个以上数据表的查询称为连接查询
1.等值连接和自然连接查询
select student.s_no 学号,s_name 姓名,s_sex 性别,s_speciality 专业,s_dept 系别,c_name 课程名称,c_grade 课程成绩
from student,sc
where student.s_no =sc.s_no;
---------------------------------
2.自连接查询(创建同义词student2)
Sql server 2000貌似还没有创建同义词的功能,Sql server 2005才有,不知道这说法对不对!!!
select student2.s_no 学号,student2.s_name 姓名,student2.s_sex 性别,student2.s_speciality 专业,student2.s_avgrade 平均成绩
from student,student2
where student,s_name='yx'
and student.s_speciality=student2.s_speciality
-----------------------------------
3.外连接查询
select s_name 姓名,s_sex 性别,s_speciality 专业,s_dept 系别,
sc.c_name 课程姓名, sc.c_grade 课程成绩
from student left join sc on (student.s_no=sc.s_no);
以上是左外连接
把最后一句改为
from sc right join student on (student.s_no=sc.s_no);
-------------嵌套查询
使用谓词IN的嵌套查询
查出an和yo所在专业的所有学生
方法一.
第1步select student.s_speciality
from student
where student.s_name='an'or student.s_name='yo'
第2步 将第一步得到的结果放到下面
select *
from student
where student.s_speciality in ('电子信息工程','生物工程');
方法二.
select *
from student
where student.s_speciality in (
select student.s_speciality
from student
where student.s_name='an' or student.s_name='yo');
-----使用比较运算符的嵌套查询
查询所有成绩比yo低的学生
select s_no 学号,s_name 姓名,s_speciality 专业,s_avgrade平均成绩
from student
where s_avgrade<(
select s_avgrade
from student
where s_name='yo');
-----------------使用谓词EXISTS的嵌套查询
select s_no 学号,s_name 姓名,s_speciality 专业
from student
where exists(
select *
from sc
where student.s_no=s_no and c_name='算法设计与分析');
-----------------查询的集合运算
对任意两个SELECT语句进行集合运算
Union--并
Intersect--交
Except--差
select s_no 学号,s_name 姓名,s_speciality 专业,s_avgrade 平均成绩
from student
where s_speciality='计算机软件与理论'
Union------换成intersect和except都会出问题,网上查了一下说是Sql Server 2000 不支持。
select s_no 学号,s_name 姓名,s_speciality 专业,s_avgrade 平均成绩
from student
where s_avgrade>=85;
------------------SQL的数据操纵功能
将学号为‘50’,姓名‘lo’,性别‘男’,出生日期‘1987-1-1’,专业为‘计算机应用技术’,
平均成绩‘92.5’,系别‘计算机系’的学生记录插入student
insert into student(s_no,s_name,s_sex,s_brithday,s_speciality,s_avgrade,s_dept)
values('50','lo','男','1987-1-1','计算机应用技术',92.5,'计算机系')

将查询到的数据输入到另一个数据表中
1.当然首先要自己creat一个表,例如student1
2.insert into student1(s_no,s_name,s_speciality,s_avgrade,s_dept)
(select s_no,s_name,s_speciality,s_avgrade,s_dept
from student);
---SQLserver 2000 可以在企业管理器上视图建表
也可以在SQL查询分析器上写create建表
-------------------使用Update语句更新数据
将所有学生的平均成绩减5分
update student
set s_avgrade=s_avgrade-5;
将所有女生的成绩加上原来分数的1%
update student
set s_avgrade=s_avgrade+s_avgrade*1%
where s_sex='女';
如果表student中平均成绩s_avgrade是表sc中的课程成绩c_grade的平均值。
1.---创建一个用于存放中间结果的数据表tmp_table
create table tmp_table(
s_no char (8)
s_avgrade numberic(3,1)
);
2.---通过按学号分组的方法求各个学生的平均成绩,并将其学号和平均成绩存放到表tmp_table
insert into tmp_table(s_no,s_avgrade)
(select s_no,AVG(c_grade)
from sc
group by s_no);
3.---用表tmp_table中各个学生的平均成绩(tmp_table.s_avgrade)
更新student中的平均成绩(student.s_avgrade).对于没有选修的学生,其成绩为空值NULL.
Update student
Set s_avgrade=(select s_avgrade from tmp_table where s_no=student.s_no);
DROP table tmp_table;
-----使用DELETE语句删除数据
删除表student中的所有数据
DELETE
from student;
删除表中没有选修任何课程的学生
delete
from student
where s_no not in (
select s_no
from sc);
------------SQL的数据控制功能
1.授予权限
SQL语句中操作全限是由GRANT语句来完成的。
把对表student的修改,查询权限给user1,user2
grant update,select
on student
to user1,user2;
把表sc中c_no和c_name的插入权限授权给user1,并同时给他授予权限的转授权。
grant insert(c_no,c_name)
on sc
to user1
with grant option;
把对表sc和表student的全部操作权授给user1和user2
grant all privileges[给予所有的权限]
on student,sc
to user1,user2;
把对表student的查询权限授权给所有数据库用户
grant select
on student
to public;
对象 对象类型 操作权限
------------------------
基本表,列 TABLE select,insert,update,delete,alter,index,all privileges
视图 TABLE select,insert,update,delete,all privileges
数据库 Database create table
-----------------------
2.收回权限
revoke update,select
on student
to user1,user2;
收回user1,user2对student查看和修改权限
------
revoke insert(c_no,c_name)
on sc
from user1;
收回user1对sc表c_no,c_name的插入权限和权限的授予权
------
revoke select
on student
from public;
收回所有用户对student查询权限

④ 数据库的查询语句作业二

1、select * from 读者信息表
2、select * from 借书表 where 编号='2002060328'
3、select * from 图书信息表 where 书名 like '程序设计'
4、select * from 图书信息表 where 出版社='清华大学出版社' order by 单价
希望对你有帮助

⑤ 图书馆管理员利用SQL怎样用索引查询图书信息

你首要先建一个表,然后将数据录入
表中可以有多个字段,比如图书编号、图书名称、出版社等
在数据库中可以使用查询语句进行数据检索
例如
SELECT
*
FROM
图书表
WHERE
图书编号='0001'

⑥ 铃SQL语句统计图书馆一共有多少本图书

这两句都是基础SQL
select count(1) from 图书表
第二句需要几个表的连接,不清楚表结构,不能写,建议你把表结构发出来,一般应该有图书表,学生表,借阅流水表

⑦ 数据库SQL语句实现下列问题

create proc t1
@dzno int,
@num int output
as
select @num=count(书号) from 借阅 where 读者号=@dzno
return @num
go
create proc t2
@bookname varchar(50),
@author varchar(20)
as
select 出版社,单价 from 图书 a where 书名 =@bookname and 作者=@author
go
create proc t3
@readername varchar(10),
@bookno int
as
declare @num int,@readerno int
select @readerno=读者号 from 读者 where 姓名=@readername
exec t1 @readerno,@num output

if @num>=(select 允许借阅册数 from 读者类别 a, 读者 b where a.姓名=@readername and a.读者类别编号=b.读者类别编号)
begin
print '超出允许借阅册数,不能再借了'
return
end
declare @days int
select @days=允许借阅天数 from 读者类别 a, 读者 b where a.姓名=@readername and a.读者类别编号=b.读者类别编号
print '借阅成功!'
insert into 借阅 values( @readerno,@bookno,getdate(),dateadd(dd,@days,getdate())

⑧ 有“图书管理”数据库,包含如下的表结构,根据要求写出相应的SQL语句。

1.select 书号,单价 from 图书 where 出版单位='清华大学出版社' order by 单价
2.select count(*) from 读者
3.insert into 借阅 values ('JSJ001','CJ005','2010-11-11')
4.update 图书 set 单价=单价*0.1
5.select 出版单位,sum(单价)/count(单价) from 图书 group by 出版单位

⑨ SQL 图书管理系统的查询语句

1. 求总藏书量、藏书总金额,总库存册数、最高价、最低价。
select count(图书编号) as 总藏书量,
sum(定价) as 藏书总金额,
sum(实际数量) as 总库存册数,
max(定价) as 最高价,
min(定价) as 最低价
from 图书卡片
go
2. 列出藏书在10本以上的书(书名、作者、出版社、年份)。
select 图书名称,作者姓名,出版社,出版日期
from 图书卡片
group by 图书编号 having(coung(1)>10)
order by 图书名称
go
3. 哪些出版社的藏书种类数超过100种。
select 出版社 as '藏书种类数超过100种的出版社'
from 图书卡片
group by 出版社 having(count(类别)>100)
order by 出版社
go
4. 目前实际已借出多少册书?
select sum(借出数量) as '借出数量'
from 图书卡片
go
5. 年份最久远的书。
select top 1 with ties 图书名称 from 图书卡片
order by 出版日期
go
6. “数据库系统原理教程,王珊编,清华大学出版社,1998年出版”还有几本?
select count(1) from 图书卡片
where concaints(摘要,'"数据库系统原理教程,王珊编,清华大学出版社,1998年出版"')
go
7. 哪一年的图书最多?
select top 1 with ties convert(substring(出版日期,1,4)) as 年份,count(1) as '图书数量'
from 图书卡片
group by 出版日期
order by 图书数量 desc
go
8. 哪本借书证未归还的图书最多?
select top 1 with ties A.读者编号,count(1) as '借书数量'
from 图书卡片 A,借阅 B
where A.图书编号=B.图书编号
group by A.读者编号
order by 借书数量 desc
go
9、平均每本借书证的借书册数。
select avg(借阅数量) as '平均每本借书证的借书册数'
from 借阅
go

10.哪个系的同学平均借书册数最多?
select top 1 with ties A.工作单位,avg(借阅数量) as '平均借阅数量'
from 读者 A,借阅 B
where A.读者编号=B.读者编号
group by A.工作单位
order by 平均借阅数量' desc
go

11. 最近两年都未被借过的书。
select 图书名称
from 图书卡片
where 图书编号 in(select 图书编号 from 借阅 where datediff(year,借阅日期,getdate())>2)
go
12. 列出那些借了图书逾期未归还的借书证号和图书名。
select A.读者编号 as '借书证号',B.图书名称
from 读者 as A inner join 图书卡片 as B on A.图书编号=B.图书编号
where A.应归还日期<getdate() and A.实际归还日期 is null
go
13.今年未借过书的借书证。
select 读者编号
from 读者
where 读者编号 not in(select 读者编号
from 读者
where datediff(year,借阅日期,getdate())=0)
go

14. 今年那种书出借最多?
select top 1 with ties A.类别,count(1) as '借出数量'
from 图书卡片 A,借阅 B
where datediff(year,B.借阅日期,getdate())=0
group by A.类别
order by 借出数量' desc
go