[点晴永久免费OA]SQL SERVER 中 select,update,delete使用表别名
				
									
					
					
						|  | 
							admin 2022年12月17日 11:3
								本文热度 2613 | 
					
				 
				【select】
 【update】
| 1 | update表别名 set表别名.列=值 from表名 表别名 where条件
 | 
 【delete】
| 1 | delete表别名 from表名 表别名 where条件
 | 
该文章在 2022/12/17 11:03:54 编辑过
|  |  | 
| 全部评论1 | 
	|  | admin 2022年12月17日 11:4 
			之所以要用别名,是因为delete的where条件中需要用到子查询写一些条件,举例:利用自连接删除表中重复的数据:
 create TABLE [dbo].[products1] (
    [rowid] [int] primary key IDENTITY(1,1) NOT NULL,
    [name] [nchar](10) NULL,
    [price] [int] NULL
)
insert INTO Products VALUES('苹果',    50);
insert INTO Products VALUES('橘子',    100);
insert INTO Products VALUES('橘子',    100);
insert INTO Products VALUES('橘子',    100);
insert INTO Products VALUES('香蕉',    80);
insert INTO Products VALUES('香蕉',    80);delete products from products as p1 where p1.rowid<
(
    select MAX(p2.rowid) from products p2 
    where p1.name=p2.name and p1.price=p2.price
)
--结果:
--    rowid        name    price
--      1            苹果        50
--      4            橘子        100
--      6            香蕉        80 该评论在 2022/12/17 11:05:12 编辑过
 |