Update
Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete居然支持inner join的update方式,这个在表间关联来做更新和删除操作非常有用.
列子:
Sql代码
update tb_User
set pass=''
from tb_User usr
inner join tb_Address addr on usr.nAddressFK = addr.nAddressID
where usr.id=123
update tb_User
set pass=''
from tb_User usr
inner join tb_Address addr on usr.nAddressFK = addr.nAddressID
where usr.id=123update的格式是
update t1 set t1.name=’Liu’ from t1 inner join t2 on t1.id = t2.tid
MYSQL,ACCESS 写法如下:
Sql代码
UPDATE mem_world AS mw1 INNER JOIN mem_world AS mw2
ON mw1.parentid = mw2.wid
SET mw1.level = mw2.level
WHERE mw2.baseid = 107
AND mw2.parentid = 0
AND mw2.size > 1;
UPDATE mem_world AS mw1 INNER JOIN mem_world AS mw2
ON mw1.parentid = mw2.wid
SET mw1.level = mw2.level
WHERE mw2.baseid = 107
AND mw2.parentid = 0
AND mw2.size > 1;
on是表连接的筛选条件
就是说,表连接后,会产生一个类似于临时的视图这么一个东西
where是从这个临时的视图中筛选数据的
所以,你首先要搞清,你的所谓的2个条件属于哪一种
Delete
delete 语句也是类似
delete from t1 from t1 inner join t2 on t1.id = t2.tid
注意蓝色部分。
mysql:
Sql代码
DELETE mwb FROM mem_world_building AS mwb INNER JOIN mem_world AS mw
ON mwb.wid = mw.wid
where mw.type between 11 and 15
and baseid = 107
and mw.parentid <> 0
and mw.size > 1;
DELETE mwb FROM mem_world_building AS mwb INNER JOIN mem_world AS mw
ON mwb.wid = mw.wid
where mw.type between 11 and 15
and baseid = 107
and mw.parentid <> 0
and mw.size > 1;
下面是ORACLE的:
Sql代码
DELETE TABLE1 where exists ( select 1 from table2 where and table1.khid=table2.khid and FWDWID=8);
DELETE TABLE1 where exists ( select 1 from table2 where and table1.khid=table2.khid and FWDWID=8);
Sql代码
DELETE TABLE1 where KHID exists ( select KHID from table2 where FWDWID=8)
DELETE TABLE1 where KHID exists ( select KHID from table2 where FWDWID=8)
分享到:
相关推荐
### SQL Server 中 DELETE 语句结合 INNER JOIN 的应用 #### 背景介绍 在数据库管理与维护过程中,经常会遇到需要删除表中的某些记录的情况。简单地使用 `DELETE` 语句可以删除单个表中的数据,但在多表关联的情况...
DELETE语句结合INNER JOIN则用于删除那些满足特定条件的记录。例如: ```sql DELETE FROM jx_lsjl jx INNER JOIN isa_gtxx gt ON jx.ckid = gt.gtbh WHERE gt.jgm='27010825' AND jx.jgm='27010825'; ``` 这条语句...
在复杂的删除场景中,可能需要使用高级连接查询,如`INNER JOIN`,`LEFT JOIN`,`RIGHT JOIN`等,以及嵌套查询(内部查询): ```sql DELETE orders,items FROM orders INNER JOIN items ON orders.orderid = ...
AND COUNT(DISTINCT SC.C#) = (SELECT COUNT(*) FROM (SELECT DISTINCT C.C# FROM SC AS SC4 INNER JOIN Course AS C ON SC4.C# = C.C# WHERE SC4.S# = '1002') AS C); ``` #### 15. 删除学习“叶平”老师课的SC...
- `DELETE SC FROM SC INNER JOIN Student ON SC.Sno = Student.Sno`:这里使用了内连接(INNER JOIN),连接条件为学生表(Student)和选课记录表(SC)的学号(Sno)相等。 - `WHERE Student.Sdept = '信息管理系'...
生的年龄增加1岁update studentset 年龄 = age + 1(19)将王欣从学生关系及选课关系中删掉delete from studentwhere 姓名 = '王欣'-- 删除选课关系中对应记录delete from xuankewhere 学号 in (select 学号 from ...
INNER JOIN Person.StateProvince sp --内联接 ON sp.StateProvinceID = a.StateProvinceID INNER JOIN Person.CountryRegion cr ON cr.CountryRegionCode = sp.CountryRegionCode WHERE a.City='Bothell' -- --...
本篇主要探讨T-SQL中的多表连接查询,包括内连接(INNER JOIN)、左连接(LEFT JOIN)、右连接(RIGHT JOIN)、全连接(FULL JOIN)以及子查询和UNION操作。 首先,多表连接查询的目标是将多个表中的相关数据联接在一起,以...
SQL 语句:SELECT * FROM tbl_students INNER JOIN tbl_score ON tbl_students.id = tbl_score.username 二、删除冗余数据 删除冗余数据是SQL开发中经常遇到的问题,需要使用subquery来删除重复的记录。例如题目...
INNER JOIN 学习 ON 学生.学号 = 学习.学号 INNER JOIN 课程 ON 课程.课程ID = 学习.课程ID; ``` (3) **查询成绩表中成绩大于平均成绩的信息**: ```sql SELECT 学习.* FROM 学习 WHERE 学习.成绩 > ...
SQLite 支持多种类型的 JOIN 操作,如 INNER JOIN、LEFT JOIN、RIGHT JOIN 和 FULL OUTER JOIN。以下是一个简单的 INNER JOIN 示例,假设我们还有一个 "Courses" 表: ```sql CREATE TABLE Courses ( id INTEGER ...
DELETE FROM STUDENTS WHERE SID NOT IN (SELECT DISTINCT STUDENTS.SID FROM STUDENTS INNER JOIN CHOICES ON STUDENTS.SID = CHOICES.SID) ``` 10. 删除不及格选课记录:删除CHOICES表中分数低于60的记录。 ```...
DELETE FROM BOOKS WHERE BNO NOT IN (SELECT DISTINCT BNO FROM BORROW) ``` 9. 查询当前同时借有"计算方法"和"组合数学"两本书的读者,输出其借书卡号,并按卡号升序排序。可以使用INTERSECT操作符或子查询,如下...
DELETE FROM BOOKS WHERE BNO NOT IN (SELECT BNO FROM BORROW) ``` 10. 如果经常按书名查询图书信息,可以在BOOKS表上建立书名的索引,以提高查询效率。 11. 在BORROW表上建立一个触发器,当读者借阅"数据库...
SELECT DISTINCT S1.Name, S1.Gender FROM Scientist S1 INNER JOIN Scientist_Projects SP1 ON S1.EmployeeID = SP1.EmployeeID INNER JOIN Scientist_Projects SP2 ON SP1.ProjectID = SP2.ProjectID INNER JOIN ...
主要有四种类型的JOIN:INNER JOIN、LEFT JOIN (或 LEFT OUTER JOIN)、RIGHT JOIN (或 RIGHT OUTER JOIN) 和 FULL JOIN (或 FULL OUTER JOIN)。其中,INNER JOIN返回两个表中匹配的行,LEFT JOIN返回左表的所有行和...
接着,`DELETE FROM t1 FROM t1 s INNER JOIN w ON s.Id=w.Id`这部分是实际的删除操作。这里使用了`INNER JOIN`来连接`t1`表(用别名`s`表示)和CTE`w`,基于它们的`Id`字段相等。由于`JOIN`操作只会匹配`w`中的行...
例如,使用INNER JOIN连接Employees和Departments两个表:SELECT E.Name, D.DepartmentName FROM Employees E INNER JOIN Departments D ON E.DepartmentID = D.DepartmentID,这将显示员工姓名及其所在部门名称。...