1、SQL 2000中创建视图的语句后可以加分号吗???2、查询至少用了供应商S1所供应的所有零件的工程号JNO,用带EXISTS谓词的子查询实现
select distinct jno
from spj spj1
where not exists
(select *
from spj spj2
where spj2.sno='s1'
and not exists
(select *
from spj spj3
where spj3.jno=spj1.jno
and spj3.pno=spj2.pno)) ;最好说清楚第一个问题的原因,,,第二个最好分析下各个语句的作用 先谢过啦!!!
select distinct jno
from spj spj1
where not exists
(select *
from spj spj2
where spj2.sno='s1'
and not exists
(select *
from spj spj3
where spj3.jno=spj1.jno
and spj3.pno=spj2.pno)) ;最好说清楚第一个问题的原因,,,第二个最好分析下各个语句的作用 先谢过啦!!!
现有两张表
create table C(C# int,CN varchar)
create table SC(S# int,C# int,G int) 表中数据如下
C表
C# CN
1 厚黑学
2 查询基础
3 能说
4 会道 http://topic.csdn.net/u/20080313/21/acb7f550-8ce7-4352-96dc-4715a0e43287.html?398509657SC表
S# C# G
1 1 1
1 3 6
2 1 75
2 2 55
2 3 7
2 4 7
3 3 77
4 1 75
4 2 55
4 3 7
4 4 7
6 6 6
7 4 7 要求显示出SC表中的C#包含C表中所有的C#的行 即结果是
2 1 75
2 2 55
2 3 7
2 4 7
4 1 75
4 2 55
4 3 7
4 4 7 -----------------------------------------------
create table C(C# int, CN varchar(10))
insert into C values(1, '厚黑学')
insert into C values(2, '查询基础')
insert into C values(3, '能说')
insert into C values(4, '会道')
create table SC(S# int, C# int, G int)
insert into SC values(1 ,1 ,1 )
insert into SC values(1 ,3 ,6 )
insert into SC values(2 ,1 ,75)
insert into SC values(2 ,2 ,55)
insert into SC values(2 ,3 ,7 )
insert into SC values(2 ,4 ,7 )
insert into SC values(3 ,3 ,77)
insert into SC values(4 ,1 ,75)
insert into SC values(4 ,2 ,55)
insert into SC values(4 ,3 ,7 )
insert into SC values(4 ,4 ,7 )
insert into SC values(6 ,6 ,6 )
insert into SC values(7 ,4 ,7 )
goselect * from sc aa where not exists
(select 1 from c a left join (select * from sc where s#=aa.s#) b on a.c#=b.c#
where b.c# is null)/*
S# C# G
----------- ----------- -----------
2 1 75
2 2 55
2 3 7
2 4 7
4 1 75
4 2 55
4 3 7
4 4 7(所影响的行数为 8 行)
*/--------------------------------------------------------------
--1、取出所有的S#
select distinct S# from SC
/*
S#
-----------
1
2
3
4
6
7
(所影响的行数为 6 行)
*/--2、取出所有的S#,C#
select M.* , C.C# from (select distinct S# from SC) M,C
/*
S# C#
----------- -----------
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4
4 1
4 2
4 3
4 4
6 1
6 2
6 3
6 4
7 1
7 2
7 3
7 4
(所影响的行数为 24 行)
*/--3、按上表找出在sc表不存在的S#,C#,然后只对S#取唯一即可。C#没用了。
select distinct t.S# from (select M.* , C.C# from (select distinct S# from SC) M,C) t where not exists (select 1 from SC n where n.S# = t.S# and n.C# = t.C#)
/*
S#
-----------
1
3
6
7
(所影响的行数为 4 行)
*/--4、从sc表里的S#找出不在上表S#就是你要的结果
select sc.* from sc where S# not in (select distinct t.S# from (select M.* , C.C# from (select distinct S# from SC) M,C) t where not exists (select 1 from SC n where n.S# = t.S# and n.C# = t.C#))drop table C,SC
select *
from spj spj2
where spj2.sno='s1'
and not exists
(select *
from spj spj3
WHERE spj3.pno=spj2.pno) ;第二句,你可以先看里面这一句,然后双重否定看外面
create view grade_view(学号,姓名,选课门数,平均分,最高分,最低分)
as
select distinct student.sno, sname,count(cno),avg(grade),max(grade),min(grade)
from student,sc
where student.sno=sc.sno
group by student.sno, sname;错误提示:
服务器: 消息 170,级别 15,状态 1,过程 grade_view,行 6
第 6 行: ';' 附近有语法错误。这句加了分号就不能成功!没加就可以。。
为什么???
as
select * from tb;命令已成功完成。不知道你那是啥语法
as
select sno,pno,qty from spj;
服务器: 消息 170,级别 15,状态 1,过程 spj_view,行 3
第 3 行: ';' 附近有语法错误。
为什么我这行会出错。
你那句加了分号后可以执行啊???
as
select sno,pno,qty from spj;column 视图中的列使用的名称。仅在下列情况下需要列名:列是从算术表达式、函数或常量派生的;两个或更多的列可能会具有相同的名称(通常是由于联接的原因);视图中的某个列的指定名称不同于其派生来源列的名称。还可以在 SELECT 语句中分配列名。如果未指定 column,则视图列将获得与 SELECT 语句中的列相同的名称。 不知道是啥,我试试
as
select * from tb;
我这样都能行的说,
P:供应商S供应X
q:工程Y使用了X
Vx(P->q)
个人的做法如下
select jno
from j
where not exists
(select *
from spj x
where sno='s1' and
not exists
(select *
from spj y
where j.jno=y.jno and
x.pno=y.pno
)
)