select tblog_TblogID from GK_Tblog where tblog_Name=   and tblog_SourceID=   ;   只有一条记录  ,假如结果为 2
select tmsg_ID from GK_TblogMsg where tmsg_TblogID = 2;                  有多条记录 ,假如结果为1, 2, 3
对于上面的结果集,每一个结果:
select count(cmt_ID)  from GK_Comment where cmt_MsgID = 1;
select count(cmt_ID)  from GK_Comment where cmt_MsgID = 2;
select count(cmt_ID)  from GK_Comment where cmt_MsgID = 3;把上面三条语句的执行结果相加,就是我想要的结果
可不可以写一个嵌套的查询实现
我不想做很多次查询
就想写一个sql语句完成,嵌套能否实现
感激不尽

解决方案 »

  1.   

    select sum(num)
    from 
    (
    select count(cmt_ID) as num from GK_Comment where cmt_MsgID = 1
    union all
    select count(cmt_ID) as num from GK_Comment where cmt_MsgID = 2
    union all
    select count(cmt_ID) as num from GK_Comment where cmt_MsgID = 3
    )T
      

  2.   

    select count(cmt_ID) 
    from GK_Comment 
    where cmt_MsgID in(select tblog_TblogID 
                       from GK_Tblog where tblog_Name= and tblog_SourceID= )
      

  3.   

    select count(cmt_ID) 
    from GK_Comment 
    where cmt_MsgID in(select tmsg_ID 
    from GK_TblogMsg 
            where tmsg_TblogID in (select tblog_TblogID 
                                   from GK_Tblog 
                                     where tblog_Name= and tblog_SourceID= );
      

  4.   

    select count(cmt_ID)  from GK_Comment where cmt_MsgID in (select tmsg_ID from GK_TblogMsg where tmsg_TblogID = 2)
      

  5.   

    select count(cmt_ID) from GK_Comment c inner join (
    select tmsg_ID from GK_Tblog a
    inner join GK_TblogMsg b
    on a.tblog_TblogID=b.tmsg_TblogID
    where tblog_Name= and tblog_SourceID= ) d
    on d.cmt__MsgID=c.tmsg_ID
      

  6.   

      不好意思啊,结贴完了,版主写的很好,sql语句好久没写了,很多都忘了,谢谢大家。