来自下面的2个SQL Relational表。我想向看过视频但每天都没有使用T-SQL或MYSQL付费的人
付款表,如下所示:该表包含日期和会员级别的所有付款记录。这是针对每天付款的会员。样本数据如下所示:
Date memID Amount
1/5 Abc $30
1/10 def $50
1/27 hij $15
用户表:此表包含日期和成员级别的所有活动记录。适用于每天访问abcvideo.com网站的成员。样本数据如下所示:
Date memID
1/1 Abc
1/5 Abc
1/5 Bac
1/10 Abc
1/10 def
1/15 Bac
我想出了一个非常大的代码,但逻辑却很小,以至于我无法理解。
SELECT Date, memberID
FROM User
WHERE Date NOT IN (SELECT Date FROM Payment )
UNION
SELECT Date,memberID
FROM User
WHERE memberID NOT IN (SELECT memberID FROM Payment)
UNION
SELECT Date, memberID
FROM User
WHERE Date IN (SELECT Date from Payment where user.memberID <> payment.memberID)
寻找更简单,更好的逻辑。
答案 0 :(得分:2)
假设您的工资是每天,那么您可能会寻找类似的东西
select *
from user u
where not exists(
select 1
from payment p
where u.memId = p.memId and
u.Date = p.Date
)
旁注:将名称分配给作为关键字(user
和Date
)的表和属性不是一个好主意。
答案 1 :(得分:0)
SELECT Date, memberID
FROM User
WHERE (Date NOT IN (SELECT Date FROM Payment ))
OR (memberID NOT IN (SELECT memberID FROM Payment))
OR (Date IN (SELECT Date from Payment where user.memberID <> payment.memberID))
答案 2 :(得分:0)
您可以使用此:
SELECT u.memid, u.date_col
FROM user_table u
LEFT JOIN payment p
ON p.date_col = u.date_col AND p.memid = u.memid
WHERE p.date_col IS NULL
ORDER BY u.memid, u.date_col;