第二个:
select a.shopid
a.shopname,
b.qty as '商铺内商品数量'
from 表1 a
left join (select shopid,count(*) as 'qty' from 表2 group by shopid) b on a.shopid=b.shopid
select a.shopid
a.shopname,
b.qty as '商铺内商品数量'
from 表1 a
left join (select shopid,count(*) as 'qty' from 表2 group by shopid) b on a.shopid=b.shopid
表1
shopid shopname
shop1 小店1号
shop2 小店2号
shop3 小店3号
.......... 表2
itemid shopid itemname
1 shop1 商品XXX
2 shop1 商品XXX
3 shop1 商品XXX
4 shop2 商品XXX
5 shop2 商品XXX
6 shop2 商品XXX
7 shop2 商品XXX
8 shop2 商品XXX
9 shop1 商品XXX
10 shop1 商品XXX
..........
需要统计的结果:
shopid shopname 商铺内商品数量
shop1 小店1号 5
shop2 小店2号 5
shop3 小店3号 0
.........
以上都是在分页中想用到的,
用存储过程,在线等!谢谢select a.* , count(*) 商铺内商品数量 from 表1 a , 表2 b where a.shopid = b.shopid group by a.shopid,b.shopname
参考:create table tb(id int, name varchar(10), pid int, px int)
insert into tb values(0 , '栏目分类', 0 , 1)
insert into tb values(1 , '动物' , 0 , 1)
insert into tb values(2 , '视频' , 0 , 2)
insert into tb values(3 , '老虎' , 1 , 1)
insert into tb values(4 , '狮子' , 1 , 2)
insert into tb values(5 , '搞笑' , 2 , 1)
go--查询指定节点及其所有子节点的函数
CREATE FUNCTION f_Cid(@ID int) RETURNS @t_Level TABLE(ID int,Level int)
AS
BEGIN
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level
FROM tb a,@t_Level b
WHERE a.PID=b.ID
AND b.Level=@Level-1
END
RETURN
END
GO --调用函数查询id = 1及其所有子节点
SELECT a.* FROM tb a,f_Cid(1) b WHERE a.ID=b.ID
/*
id name pid px
----------- ---------- ----------- -----------
1 动物 0 1
3 老虎 1 1
4 狮子 1 2
(所影响的行数为 3 行)
*/
drop table tb
drop function dbo.f_cid
1 0 体育用品 2
2 0 家用电器 3
第二个就是一个Group By,参考前面的:select a.* , count(*) 数量 from tblA a , tblB b where a.shopid = b.shopid group by a.shopid,b.shopname
--第1题:
create table 表1(pid int identity(1,1),parent int,desn varchar(50))
insert into 表1 select 0,'体育用品'
insert into 表1 select 0,'家用电器'
insert into 表1 select 1,'足球'
insert into 表1 select 1,'篮球'
insert into 表1 select 3,'阿迪达斯足球'
insert into 表1 select 2,'电视机'
insert into 表1 select 2,'冰箱'
insert into 表1 select 6,'海尔电视机'
insert into 表1 select 7,'海尔冰箱'
create table 表2(itemid int identity(1,1),pid int,name varchar(50))
insert into 表2 select 5,'1型号足球'
insert into 表2 select 9,'双开门冰箱'
insert into 表2 select 4,'牛皮篮球'
insert into 表2 select 9,'车用冰箱'
insert into 表2 select 6,'液晶电视'
create function f_cid(@pid int)
returns varchar(500)
as
begin
declare @t table(pid int,parent int,desn varchar(50),lev int)
declare @lev int
set @lev=1
insert into @t select *,@lev from 表1 where pid=@pid
while(@@rowcount>0)
begin
set @lev=@lev+1
insert into @t select a.*,@lev from 表1 a,@t b
where a.parent=b.pid and b.lev=@lev-1
end
declare @cids varchar(500)
select @cids=isnull(@cids+',','')+ltrim(pid) from @t order by lev
return @cids
end
select *,
商品count=(select count(1) from 表2 where charindex(','+ltrim(pid)+',',','+dbo.f_cid(a.pid)+',')>0)
from 表1 a
我要的效果在给大家解释一下吧
需要统计结果为:
pid parent desn 商品count
1 0 体育用品 2
2 0 家用电器 3
3 1 足球 1
4 1 篮球 1
5 3 阿迪达斯足球 1
6 2 电视机 1
7 2 冰箱 2
8 6 海尔电视机 1
9 7 海尔冰箱 1 体育用品 是最高级的一个类别 他下面有pid 为3和4,3下面有5,而5在表2里头就1个记录,所以统计这个类别为1个记录,同理,pid为4的在表2里头也有1个记录,这样算回来,体育用品下头的总商品就为2,其他同理。简单的说,我就是想要每种类别下面,他的商品总数是多少