我必须与用户一起表。两者都有一个邮件列和一个移动列。我必须找到table1中的所有行,其中mail和mobile都不在table2中。表1中的邮件和移动设备不为空。
该对象是从table1中查找table2中尚未存在的用户。我们使用邮件或手机来识别它们。
我使用MySQL
table1
|mail |mobile |
|a@mail.com |12345678|
| |21345678|
|c@mail.com |84726287|
table2
|mail |mobile |
|d@mail.com |12345678|
|q@mail.com |21888888|
|c@mail.com | |
Result
|a@mail.com |12345678|
| |21345678|
答案 0 :(得分:1)
如果您想查找其他表中没有邮件且没有移动设备的用户:
SELECT t1.*
FROM dbo.Table1 t1
WHERE NOT EXISTS
(
SELECT 1
FROM dbo.Table2 t2
WHERE t1.Mail = t2.Mail
)
AND NOT EXISTS
(
SELECT 1
FROM dbo.Table2 t2
WHERE t1.Mobile = t2.Mobile
)
如果两者必须在同一行上匹配,则更简单:
SELECT t1.*
FROM dbo.Table1 t1
WHERE NOT EXISTS
(
SELECT 1
FROM dbo.Table2 t2
WHERE t1.Mail = t2.Mail AND t1.Mobile = t2.Mobile
)
答案 1 :(得分:0)
select a.mail, a.mobile
from table1 a
left outer join table2 b on a.mail = b.mail and a.mobile = b.mobile
where b.mail is null
and a.mail <> ''
and a.mobile <> ''