我有列 ID,USERSID 表的用户表:
t_A(ID,usersID,ADATE,priceA,priceB)
t_B(ID,usersID,BDATE,priceA,priceB)
t_C(ID,usersID,CDATE,priceA,priceB)
我正在使用此查询从 X DATE 和 USERSID 的 3 个表中获取价格总和
declare @id int
set @id = 3 -- for example
SELECT SUM(priceA) as TA, SUM(priceB) as TB
FROM t_A,t_B,t_C
WHERE t_A.USERSID = @id
AND t_B.USERSID = @id
AND t_C.USERSID = @id
AND ADATE >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, getdate()))
AND BDATE >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, getdate()))
AND CDATE >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, getdate()))
此脚本仅在用户 ID 在三个表中有一行时才有效,否则脚本不返回任何内容