Delete from join 用法

Posted 代码艺术

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delete from join 用法相关的知识,希望对你有一定的参考价值。

delete (别名) from tblA (别名) left join tblb (别名) on。。。用法

 

1、创建使用的表及数据

CREATE TABLE YSHA 
(
    code VARCHAR(10),
    NAME VARCHAR(20)
)

CREATE TABLE YSHB
( 
   code VARCHAR(10),
   col VARCHAR(10)
)

INSERT INTO YSHA VALUES (1,A1)
INSERT INTO YSHA VALUES (2,A2)

INSERT INTO YSHB VALUES(1,Row1)
INSERT INTO YSHB VALUES(1,Row2)
INSERT INTO YSHB VALUES(1,Row3)
INSERT INTO YSHB VALUES(3,Row1)

2、delete from left join

DELETE A FROM YSHA A LEFT JOIN YSHB B ON A.code=b.code WHERE b.code is NULL
SELECT * FROM YSHA
SELECT * FROM YSHB
DELETE FROM YSHA WHERE NOT EXISTS(SELECT 1 FROM YSHB B WHERE YSHA.code=b.code )
SELECT * FROM YSHA
SELECT * FROM YSHB

 

以上是关于Delete from join 用法的主要内容,如果未能解决你的问题,请参考以下文章

mysql中删除表记录delete from和truncate table的用法区别

mysql from 逗号 是join的简写吗

SQL的JOIN用法总结

Sql inner join

SQL——delete left join

mssql sqlserver update delete表别名用法简介