日期:2014-05-19  浏览次数:20670 次

关联表取数据,有多余数据,求正确语句
/*表   :宾馆_客房
id           KeyId         客房类型     价格  
1 4 标准间 180元/天
2 4 单人间 100元/天
3 3 总统豪华 10000元/天
4 3 商务客房 2000元/天
5 4 三人间 200元/天 NULL
表:图片信息
picid     subid         KeyId       picname                     filename                   servefile
29 4 4 标准客房     20070612.jpg D:\projects\map\map\pictures\20070612.jpg image/jpeg
30 4 3 总统豪华客房 2007061202.jpg D:\gby\test\map\pictures\2007061202.jpg image/jpeg
31 4 3 商务套房   2007061203.jpg D:\gby\test\map\pictures\2007061203.jpg image/jpeg
32 5 3 名店廊                 2007061207.jpg D:\gby\test\map\pictures\2007061207.jpg image/jpeg

想要得到以下内容:
picid       picname               客房类型     价格                   serverfile                
30 总统豪华客房       总统豪华     10000元/天     D:\gby\test\map\pictures\2007061202.jpg
31 商务套房             商务客房     2000元/天       D:\gby\test\map\pictures\2007061203.jpg

create   table   宾馆_客房
(id   int   not   null,
KeyId   int   not   null,
客房类型   nvarchar(20)   null,
价格   nvarchar(30)   null)
create   table   图片信息
(PicId   int   not   null,
SubId   int,
KeyId   int,
PicName   nvarchar(50),
serverFile   nvarchar(50))*/

------解决方案--------------------
select a.picid,a.picname,b.客房类型,b.价格,a.serverfile
from 图片信息 a,宾馆_客房 b
where a.keyid=b.keyid and
(select count(*) from 图片信息 where keyid=a.keyid and picid <a.picid)=
(select count(*) from 宾馆_客房 where keyid=b.keyid and id <b.id)