服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Sql Server - SQL学习笔记五去重,给新加字段赋值的方法

SQL学习笔记五去重,给新加字段赋值的方法

2019-12-06 14:20MSSQL教程网 Sql Server

SQL学习笔记五去重,给新加字段赋值的方法,需要的朋友可以参考下。

去掉数据重复 
增加两个字段 
alter TABLE T_Employee Add FSubCompany VARchar(20); 
ALTER TABLE T_Employee ADD FDepartment VARCHAR(20); 

给新加的字段赋值 
UPDATE T_Employee SET FSubCompany='Beijing',FDepartment='Development' where FNumber='DEV001'; 
UPDATE T_Employee SET FSubCompany='ShenZhen',FDepartment='Development' where FNumber='DEV002'; 
UPDATE T_Employee SET FSubCompany='Beijing',FDepartment='HumanResource' where FNumber='HR001'; 
UPDATE T_Employee SET FSubCompany='Beijing',FDepartment='HumanResource' where FNumber='HR002'; 
UPDATE T_Employee SET FSubCompany='Beijing',FDepartment='InfoTech' where FNumber='IT001'; 
UPDATE T_Employee SET FSubCompany='ShenZhen',FDepartment='InfoTech' where FNumber='IT002'; 
UPDATE T_Employee SET FSubCompany='Beijing',FDepartment='Sales' where FNumber='SALES001'; 
UPDATE T_Employee SET FSubCompany='Beijing',FDepartment='Sales' where FNumber='SALES002'; 
UPDATE T_Employee SET FSubCompany='ShenZhen',FDepartment='Sales' where FNumber='SALES003'; 

查询并去重 
select distinct FDepartment from T_Employee 

select distinct FDepartment,FSubCompany from T_Employee

延伸 · 阅读

精彩推荐