22,209
社区成员
发帖
与我相关
我的任务
分享
select lp.ID,lp.Title,count(pic.ID)as Pic
from tb_Loupan lp left join tb_LoupanPics pic on pic.LpId=lp.ID
group by lp.ID,lp.Title
----=======
select lp.ID,lp.Title,Count(ihx.HxId)as Huxing
from tb_Loupan lp left join tb_LoupanInHuxing ihx on ihx.LpId=lp.ID
group by lp.ID,lp.Title
-- ----============
select lp.ID,lp.Title,Count(dp.ID)as Dianping
from tb_Loupan lp left join tb_LoupanDianping dp on dp.LpId=lp.ID
group by lp.ID,lp.Title
----===================
select lp.ID,lp.Title,count(yx.ID)as Yixiang
from tb_Loupan lp left join tb_LoupanYixiang yx on yx.LpId=lp.ID
group by lp.ID,lp.Title
select
id,title,
sum(case when pic is not null then 1 else 0 end) as pic ,
sum(case when Hx is not null then 1 else 0 end) as Hx ,
sum(case when Dp is not null then 1 else 0 end) as Dp ,
sum(case when Yx is not null then 1 else 0 end) as Yx
from
(
select lp.ID,lp.Title,pic.ID as Pic,ihx.HxId as Hx,dp.ID as Dp,yx.ID as Yx
from tb_Loupan lp
left join tb_LoupanPics pic on pic.LpId=lp.ID
left join tb_LoupanInHuxing ihx on ihx.LpId=lp.ID
left join tb_LoupanDianping dp on dp.LpId=lp.ID
left join tb_LoupanYixiang yx on yx.LpId=lp.ID
order by lp.AddTime desc
) t
group by
id,title
;with f as
(
select lp.ID,lp.Title,pic.ID as Pic,ihx.HxId as Hx,dp.ID as Dp,yx.ID as Yx
from tb_Loupan lp
left join tb_LoupanPics pic on pic.LpId=lp.ID
left join tb_LoupanInHuxing ihx on ihx.LpId=lp.ID
left join tb_LoupanDianping dp on dp.LpId=lp.ID
left join tb_LoupanYixiang yx on yx.LpId=lp.ID
order by lp.AddTime desc
)
select
id,title,
sum(case when pic is not null then 1 else 0 end) as pic ,
sum(case when Hx is not null then 1 else 0 end) as Hx ,
sum(case when Dp is not null then 1 else 0 end) as Dp ,
sum(case when Yx is not null then 1 else 0 end) as Yx
from
f
group by
id,title
在另一貼已回復了
select lp.ID,lp.Title,
COUNT(DISTINCT pic.ID) as Pic,
COUNT(DISTINCT ihx.HxId) as Hx,
COUNT(DISTINCT dp.ID) as Dp,
COUNT(DISTINCT yx.ID) as Yx
from tb_Loupan lp
left join tb_LoupanPics pic on pic.LpId=lp.ID
left join tb_LoupanInHuxing ihx on ihx.LpId=lp.ID
left join tb_LoupanDianping dp on dp.LpId=lp.ID
left join tb_LoupanYixiang yx on yx.LpId=lp.ID
GROUP BY lp.ID,lp.Title,
select t1.id , t1.title , t1.pic , t2.huxing hx , t3.dianping dp , t4.yixiang yx from
(
select lp.ID,lp.Title,count(pic.ID)as Pic
from tb_Loupan lp left join tb_LoupanPics pic on pic.LpId=lp.ID
group by lp.ID,lp.Title
) t1,
(
select lp.ID,lp.Title,Count(ihx.HxId)as Huxing
from tb_Loupan lp left join tb_LoupanInHuxing ihx on ihx.LpId=lp.ID
group by lp.ID,lp.Title
) t2,
(
select lp.ID,lp.Title,Count(dp.ID)as Dianping
from tb_Loupan lp left join tb_LoupanDianping dp on dp.LpId=lp.ID
group by lp.ID,lp.Title
) t3,
(
select lp.ID,lp.Title,count(yx.ID)as Yixiang
from tb_Loupan lp left join tb_LoupanYixiang yx on yx.LpId=lp.ID
group by lp.ID,lp.Title
) t4
where t1.id = t2.id and t1.id = t3.id and t1.id = t4.id