求一SQL语句!

fusoft 2008-01-15 05:28:19
表 Album(相册)

ID Name PicCount
---- ---- ----
1 aaa 0
2 bbb 2
3 ccc 1
...

表 Photo (图片)

ID AlbumID Name
---- ---- ----
1 2 aaa.jpg
2 2 bbb.jpg
3 3 ccc.jpg

读取表Album所有相册列表
如果该相册PicCount >0 则 从表Photo中按ID排顺序取出一张图片的Name
如果该相册PicCount =0 则 返回Null
...全文
76 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
昵称被占用了 2008-01-15
  • 打赏
  • 举报
回复

select a.* ,b.Name
from Album a left join Photo B on a.ID= AlbumID and a.PicCount >0
and not exists (select 1 from Photo where AlbumID=b.AlbumID and id<b.Id)
昵称被占用了 2008-01-15
  • 打赏
  • 举报
回复

select a.* ,b.Name
from Album a left join Photo B on a.ID= AlbumID and a.PicCount >0

pt1314917 2008-01-15
  • 打赏
  • 举报
回复

select a.id,a.name,case when a.piccount>0 then c.name else null end
from album a,(select * from photo b where not exists(select 1 from photo where albumid=b.albumid and id<a.id))c where a.id=c.albumid
xiaoku 2008-01-15
  • 打赏
  • 举报
回复
select a.* ,b.Name  
from Album a left join (select AlbumID ,name from Photo t where not exists (select 1 from Photo where t.AlbumID=AlbumID and a.ID>ID )) b on a.ID= AlbumID
-狙击手- 2008-01-15
  • 打赏
  • 举报
回复
select a.* ,
case when piccount > 0 then (select top 1 name from photo where albumid = a.id order by id)
else null
end
from album a

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧