简体   繁体   English

sql从一个表中删除并比较数据另一个表都没有PK,FK关系

[英]sql deleat from one table with compare data another table both don't have PK,FK relation

我有两个表名 tblstu 和 tblmark tblstu 有 3 条记录,分别在 tblmark 中有 pk(1,2,3) 相同的记录,但 tblmark 有 3 条额外的记录,在 tblstu 中找不到,我可以从 tblmark 那里删除不匹配的记录吗?两个表中 pk 和 fk 没有关系

ANSI SQL answer: ANSI SQL 答案:

delete from tblmark where id not IN (select id from tblstu)

If tblstu.id is nullable, adjust to:如果 tblstu.id 可为空,则调整为:

delete from tblmark where id not IN (select id from tblstu where id is not null)
DELETE FROM tblmark
WHERE NOT EXISTS (SELECT 1
                  FROM tblstu
                  WHERE tblmark.PK = tblstu.PK)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个表中返回最近插入的数据以及通过PK / FK关系链接的另一个表? - How can I return the most recently inserted data from one table in conjunction with another that is linked via a PK/FK relationship? sql选择第二个表中没有关系的记录 - sql select records that don't have relation in a second table 如何使SQL表列与另一列顺序一致,使它们都成为复合PK - How to make SQL table column ordinal to another one, making both of them a composite PK SQL-比较一张表中的数据 - SQL - Compare data from one table 如何从一个SQL表中选择未出现在另一表中的项目 - How can I select items from one sql table that don't appear in another table 如何查询由另一个pk到pk表描述的2个表的关系? - How to query 2 tables relation of which is described by another pk to pk table? 来自一个表的用户数据,相对于另一个表? - User Data from a table, in relation to another table? 如何从一个表中查找另一表中不存在的数据? - How to find data from one table which don't exist in another table? SQL-无法将数据从一个表中的一列移动到另一表中的另一列 - SQL - Can't move data from one column in one table to another column in another table 从一个表中查找在另一个表中不存在的记录,或者对于特定值总和为0 - Find records from one table which don't exist in another OR have a total sum of 0 for specific value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM