我正在尝试构建一个sql语句,用于检查重复的用户ID WITHIN TABLE1 和 TABLE2 (通过加入找到重复的用户ID不应记录表1 和 Table2 。)
以下是我的表信息和我已实施的当前检查:
表1 : UserID ,用户名。
表2 :用户ID ,状态。
表3 :用户ID ,用户名,问题
目前我只有3个SELECT语句来完成上述3个检查,并将结果插入表3中:
1.
Insert in to Table3(userid,issue)
SELECT t1.userid,'check no.1'
FROM table1 t1
FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
where t1.userid not null and t2.userid is null
2.
Insert in to Table3(userid,issue)
SELECT t1.userid,'check no.2'
FROM table1 t1
inner JOIN table2 t2 ON t1.userid = t2.userid
where t2.status = 'DELETE'
3.
Insert in to Table3(userid,issue)
SELECT t2.userid,'check no.3'
FROM table2 t2
right outer JOIN table2 t2 ON t1.userid = t2.userid
where t2.status <> 'DELETE' and t1.userid is null
现在,我想要一个第4个sql查询,它在自己的 Table1 和 Table2 中检查重复的 UserID ,而不是通过比较2个表一起,但每个表单独。
如果找到任何重复项,请将记录插入 Table3 with
ISSUE =' Table1 '中找到的重复用户ID,
ISSUE ='在 Table2中找到重复的用户ID '
谢谢你们。
答案 0 :(得分:1)
从你的问题来看,答案应该是:
insert into table3(userid,issue)
select t2.userid, 'dup user id'from table1 t1 join table2 t2
where t1.userid = t2.userid
但是我不确定你是否想要其他的东西。