首页 > 代码库 > 0903 连接查询
0903 连接查询
create database lianxi0903
go
use lianxi0903
go
create table gong
(
gcode int primary key,
gname varchar(20),
gtel int
)
go
insert into gong values(10001,‘可口可乐公司‘,1234567)
insert into gong values(10002,‘日用品公司‘,1234532)
insert into gong values(10003,‘名牌公司‘,1234545)
insert into gong values(10004,‘文具公司‘,1234565)
go
create table cang
(
ccode int primary key identity(1001,1),
cname varchar(20),
cshu int,
cprice decimal(18,2),
cg int
)
go
insert into cang values(‘可口可乐‘,4,5,10001)
insert into cang values(‘牙刷‘,5,5.5,10002)
insert into cang values(‘阿迪‘,2,225,10003)
insert into cang values(‘毛笔‘,15,5,10004)
insert into cang values(‘芬达‘,15,4.5,10001)
insert into cang values(‘牙膏‘,3,9.5,10002)
insert into cang values(‘耐克‘,2,995,10003)
insert into cang values(‘钢笔‘,12,10.5,10004)
insert into cang values(‘雪碧‘,4,3.5,10001)
insert into cang values(‘杯子‘,6,10,10002)
insert into cang values(‘乔丹‘,3,345,10003)
insert into cang values(‘铅笔‘,12,2,10004)
insert into cang values(‘可乐‘,22,5,10001)
insert into cang values(‘洗衣粉‘,12,8,10002)
insert into cang values(‘鸿星尔克‘,2,145,10003)
insert into cang values(‘中性笔‘,6,2,10004)
go
create table men
(
mcode int primary key identity(1001,1),
mname varchar(20),
mshu int,
mprice decimal(18,2)
)
go
insert into men values(‘可口可乐‘,10,5)
insert into men values(‘牙刷‘,6,5.5)
insert into men values(‘阿迪‘,2,225)
insert into men values(‘毛笔‘,10,5)
insert into men values(‘芬达‘,15,4.5)
insert into men values(‘牙膏‘,7,9.5)
insert into men values(‘耐克‘,2,995)
insert into men values(‘钢笔‘,15,10.5)
insert into men values(‘雪碧‘,5,3.5)
insert into men values(‘杯子‘,8,10)
insert into men values(‘乔丹‘,3,345)
insert into men values(‘铅笔‘,15,2)
insert into men values(‘可乐‘,20,5)
insert into men values(‘洗衣粉‘,10,8)
insert into men values(‘鸿星尔克‘,2,145)
insert into men values(‘中性笔‘,10,2)
go
select *from men
select *from cang
select *from gong
select cang.ccode,cname,cprice,gong.gname,gtel from cang
join gong on cang.cg=gong.gcode
select cang.ccode,cname,cprice,cshu from cang
union
select men.mcode,mname,mprice,mshu from men
select *from gong where gcode in(select cg from cang where cprice =(select top 1 mprice from men order by mprice))
--看看如果买六双鸿星尔克需要进多少货
declare @hshu int
declare @hshu1 int
select @hshu=mshu from men where mname=‘鸿星尔克‘
select @hshu1=cshu from cang where cname=‘鸿星尔克‘
if @hshu+@hshu1<=6
print ‘您需要进货‘+cast((6-@hshu-@hshu1) as varchar(10))+‘双鸿星尔克‘
update cang set cshu=3 where cname=‘鸿星尔克‘
declare @s int
declare @i int
set @s=0
set @i=1
while @i<=100
begin
set @s=@s+@i
set @i=@i+1
end
print @s
select cang.cname, gong.gname,gtel from cang
join gong on gong.gcode=cang.cg
where cshu<20
0903 连接查询