一个投票活动假设有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 所投的总票数
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 所投的总票数
用到多表查询 你试试吧!
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)
inner join [A]
on A.UID=c.UID and A.CITY_ID=1 and A.GROUP_ID=1
試試看吧
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
sql语句大集锦,很有收藏价值