/************************************/
然后在表C中选择c.msgid=b.msgid and c.service=b.service的数据,如果有就在循环里加1,没有不加
/************************************/
以上作何解释?如果上面条件符合的记录有多条,是不是也在循环中加多次1?
如果是:select 
     count(1)
from 
     A
inner join
     B
on
     A.mobile = B.mobile and A.service = B.service
inner join
     C
on
     B.msgid = C.msgid and B.service = C.service
where 
     A.online = 1如果不是:
select 
     count(distinct cast(A.id as varchar(10))+'||'+cast(B.id as varchar(10)))
from 
     A
inner join
     B
on
     A.mobile = B.mobile and A.service = B.service
inner join
     C
on
     B.msgid = C.msgid and B.service = C.service
where 
     A.online = 1

解决方案 »

  1.   

    zjcxc(邹建) 你好:以前你也给我解决过问题谢谢了:)
      

  2.   

    我的目的不是加一,只要能写出这样的语句,中间的我就能处理了,不是加1,是把a.mobile写入文本文件!也就是符合条件了
      

  3.   

    --如果写你这个查询,按理解,应该是这样select * from 表C c,(
    --最后一条记录的mobile,msgid两个字段
    select msgid,mobile from 表B b,(
    --在表A中选择Online=1的数据
    select distinct mobile,service from 表A where Online=1
    )a where
    --a.mobile=b.mobile and a.service=b.serviceid
    a.mobile=b.mobile and a.service=b.serviceid
    --最后一条记录
    and not exists(
    select * from B where id>b.id and msgid=a.msgid
    and a.mobile=mobile and a.service=serviceid)
    )b where c.msgid=b.msgid and c.mobile=b.mobile
      

  4.   

    条件也就是从表A中选择记录,然后在表B中选符合条件的"最近"的一条记录的字段,然后在表C中查找,如果找到就加1,找不到不加,因为表C中有也只能有一条是符合的,所以不用考略多条!===========这个我想说明白了:)
      

  5.   

    还得忙烦 zjcxc(邹建) 一下,你看我最后发的,表A中没有msgid字段,是表B中的,然后和表C中的msgid是一一对应的,三个表中的mobile都是一样的!表A字段id,mobile,service,online
    表B字段id,msgid,mobile,service
    表C字段id,msgid,mobile,error
      

  6.   

    没有,就是A和B中的mobile和service是对应的,B和C中是以msgid和mobile对应的!
      

  7.   

    select * from C c,(
                                select b.mobile,b.service,max(b.msgid) from B b,(
                                                   select a.mobile,a.service  from A a,where a.online=1
                                                   )a     where a.mobile=b.mobile and a.service=b.service 
    group by b.mobile,b.service )d 
    where d.msgid=c.msgid and d.service=c.service
    不知道這樣行不行
      

  8.   

    錯了,應該是這樣吧
    select * from C c,(
                                select b.mobile,b.service,max(b.msgid) from B b,(
                                                   select a.mobile,a.service  from A a,where a.online=1
                                                   )a     where a.mobile=b.mobile and a.service=b.service 
    group by b.mobile,b.service )d 
    where d.msgid=c.msgid and d.mobile=c.mobile
      

  9.   

    为什么不用视图呢?用a.mobile=b.mobile and a.service=b.serviceid and Online=1条件建视图1(view1),然后是不是就容易多了
      

  10.   

    select count(c.*) from 表C c,(select top 1 b.msgid,b.mobile from 表B b,表A a 
                          where a.Online=1 and a.mobile=b.mobile and 
                                a.service=b.serviceid 
                          order by b.msgid,b.mobile desc) ee
    where c.msgid=ee.msgid and c.mobile=ee.mobile不知道是不是要上面的结果,上面返回符合要求的结果数,没有则为0,楼主可以试试看
      

  11.   

    select 
         A.mobile
    from 
         A
    inner join
         A.
    on
         A.mobile = B.mobile and A.service = B.service
    inner join
         C
    on
         B.msgid = C.msgid and B.mobile= C.mobile
    where 
         A.online = 1