一个投票活动假设有A B C 三表【A--用户表】UID      (ID)
GROUP_ID (组别)
CITY_ID  (城市)【B-照片表】PID      (照片ID)
UID      (和A表UID关联)【C-投票表】UID           (和A表UID关联)
PhotoID       (和B表PID关联)
投票流程:
当用户投票成功后,往C表写入该用户的UID和所投照片的PID
求助:
需要得出 CITY_ID  (城市)=1 和 GROUP_ID (组别)=1  所投的总票数

解决方案 »

  1.   

    select count(UID)from A and B where CITY_ID  (城市)=1 and GROUP_ID (组别)=1
    用到多表查询 你试试吧!
      

  2.   

    SELECT COUNT(*) FROM A WHERE A.GROUP_ID = 1 AND A.CITY_ID = 1 AND A.UID IN (SELECT UID FROM C)
      

  3.   

    select count(*) from c,a
    where c.uid=a.uid
    and a.city_id =1 and a.group_id = 1select count(*) from c where uid in 
    (select uid from a where city_id =1 and group_id = 1)
      

  4.   

    select count(*) from  c where uid in (select uid from  a where group_id='1' and city_id='1')
      

  5.   

    select count(*) from [c]
    inner join [A]
    on A.UID=c.UID and A.CITY_ID=1 and A.GROUP_ID=1
    試試看吧
      

  6.   

    declare @A table(UID nvarchar(10),Group_ID nvarchar(10),CITY_ID nvarchar(10))
    insert into @A select 'U1','G1','C1' union all
    select 'U2','G1','C1' union all
    select 'U3','G1','C1' union all
    select 'U4','G2','C2'declare @B table(PID nvarchar(10),UID nvarchar(10))
    insert into @B select 'P1','U1' union all
    select 'P2','U1' union all
    select 'P3','U3' union all
    select 'P4','U4'declare @C table(UID nvarchar(10),PhotoID nvarchar(10))
    insert into @C select 'U1','P1' union all
    select 'U1','P1' union all
    select 'U3','P2' union all
    select 'U4','P2'
     
    select count(0) from @C c inner join @A a on a.UID = c.UID where a.Group_ID = 'G1' and CITY_ID = 'C1'结果3
      

  7.   

    http://www.lokcore.com/avrilxu/article.asp?id=23收藏吧,sql语句集锦
      

  8.   

    http://www.lokcore.com/avrilxu/article.asp?id=23
    sql语句大集锦,很有收藏价值