新人求问,急,如何查询.

answer01 2010-09-17 05:41:53
有两个表alumb(相册) 和 photo(图片),
alumb 两个字段 一个id 一个alumb_name
photo 三个字段, 一个id,一个alumb_id,一个photo_name
查询所有相册的名称,和相册里面装图片的数量.
如果相册里面没有照片,则显示相册名称,图片数量为0,

好友 2
同事 3
同学 2
老师 0

类似这样的,不知道表达清除了没有.
...全文
39 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2010-09-17
  • 打赏
  • 举报
回复
select m.alumb_name , isnull(count(1),0) 
from alumb m left join photo n
on m.id = n.alumb_id
group by m.alumb_name
willy2358 2010-09-17
  • 打赏
  • 举报
回复
select alumb_name,num=count(photo_name)
from alumb a left join photo b
on a.id=b.alumb_id
group by alumb_name
---标准答案
billpu 2010-09-17
  • 打赏
  • 举报
回复
用下嘻嘻的数据 小改个单词
if object_id('test.dbo.alumb') is not null drop table alumb
-- 创建数据表
create table alumb
(
id int,
alumb_name varchar(6)
)
go
--插入测试数据
insert into alumb select 1,'同事'
union all select 2,'好友'
union all select 3,'同学'
UNION ALL SELECT 4,'kkk'
go
if object_id('test.dbo.photo') is not null drop table photo
-- 创建数据表
create table photo
(
id int identity(1,1),
alumb_id int,
photo_name varchar(10)
)
go
--插入测试数据
insert into photo select 1,'1000.jpg'
union all select 1,'1001.jpg'
union all select 1,'1002.jpg'
union all select 2,'1.jpg'
union all select 2,'10.jpg'
union all select 3,'100.jpg'
union all select 3,'10021.jpg'
go
--代码实现
select alumb_name,num=count(photo_name)
from alumb a left join photo b
on a.id=b.alumb_id
group by alumb_name

/*
select alumb_name,num=count(photo_name)
from alumb a inner join photo b
on a.id=b.alumb_id
group by alumb_name

*/
billpu 2010-09-17
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20100917/13/8bfdea85-38b1-4a5d-a3f1-41c18ab74d3d.html
billpu 2010-09-17
  • 打赏
  • 举报
回复
这个问题似曾相识...刚看到过 穿越?

34,576

社区成员

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

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