如果 SQL 表中的数据与另一个表中的数据匹配,我需要一些帮助来删除该数据。
有两个表
Table 1: DNC
Table 2: Call_Logs
表 1 只有一列,名为 phone_number
。
表 2 有多个列,但重要的主要一列也名为 phone_number
。
基本上,我想从表 1 中删除表 2 中的任何数字(如果存在)。现在,我不想删除表 1 中的每个数字(如果它们存在于表 2 中)。我从表 2 中收集的数字基于某些标准。
要从表 2 中提取需要从表 1 中删除的数据,我使用以下命令:
select phone_number from call_logs where call_date < 'DATE' and Status = 'DNC'
此查询将为我提供一个列表,其中包含我想要从表 1 中删除的所有电话号码(如果存在)。
示例: https://drive.google.com/file/d/0B4NE4ZDXd6steW5odWhBMDJSY1U/view
我不确定如何在 SQL 中运行查询。任何类型都将不胜感激。
最佳答案
查看 img 中的示例
您可以在表 2 上使用左联接(其中 table2.phone_number 为 null 别名不匹配)
delete from table1
left join table2 on table1.phone_number = table2.phone_number
where table2.phone_number is null
关于MYSQL - 如果数字存在于另一个表中,则从 SQL 中的一个表中删除该数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44933710/