请教一个奇怪的DELETE写法

[复制链接]
查看11 | 回复7 | 2016-1-6 14:01:09 | 显示全部楼层 |阅读模式
今天朋友问我一个DELETE语句:
DELETE FROM TABLEA FROM TABLEB WHERE TABLEA.X=TABLEB.Y
我一看,就说手误吧,DELETE后面能有两个FROM吗?
他说他也是第一次看到,但是这个语句是SQLSERVER生产库里的。
请教各位达人,这语句是啥意思?
个人认为如果这语句没问题,就是级联删除TABLEA,TABLEB里满足TABLEA.X=TABLEB.Y的记录。但是两个FROM真让人百思不得其解


回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
不是吧,我感觉对应 ORACLE 语法的话应该是:
delete from tablea where exists (select 1 from tableb where tablea.x=tableb.y)
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
solomon_007 发表于 2015-4-11 15:48
不是吧,我感觉对应 ORACLE 语法的话应该是:
delete from tablea where exists (select 1 from tableb wh ...


good job
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
猫猫,CJ,你这个只是删除TABLEA里的数据,他那个是删除两张表TABLEA, TABLEB的数据
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
而且MYSQL里有类似的写法:
DELETE A.*, B.* FROM TABLEA A, TABLEB B WHERE A.X=B.Y
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
但是他这个有两个FROM
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
去SQL server版问吧,这里是Oracle,走错门了
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层

这是它的语言扩展
https://msdn.microsoft.com/en-us/library/ms189835.aspx
C. Using joins and subqueries to data in one table to delete rows in another table

The following examples show two ways to delete rows in one table based on data in another table. In both examples, rows from the SalesPersonQuotaHistory table in the AdventureWorks2012 database are deleted based on the year-to-date sales stored in the SalesPerson table. The first DELETE statement shows the ISO-compatible subquery solution, and the second DELETE statement shows the Transact-SQL FROM extension to join the two tables.





-- SQL-2003 Standard subquery

DELETE FROM Sales.SalesPersonQuotaHistory
WHERE BusinessEntityID IN
(SELECT BusinessEntityID
FROM Sales.SalesPerson
WHERE SalesYTD > 2500000.00);
GO





-- Transact-SQL extension
DELETE FROM Sales.SalesPersonQuotaHistory
FROM Sales.SalesPersonQuotaHistory AS spqh
INNER JOIN Sales.SalesPerson AS sp
ON spqh.BusinessEntityID = sp.BusinessEntityID
WHERE sp.SalesYTD > 2500000.00;
GO
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行