日期:2014-05-17  浏览次数:20817 次

sql更新语句
同一张表中有男女两个人的名字相同,现在在相同的男生姓名前加上男,女生向明前加上女的sql语句怎么写?
急求,谢谢
------解决方案--------------------

if OBJECT_ID('test') is not null
drop table test
go
create table test
(
name varchar(20),
sex varchar(2)
)
go
insert test
select '张三','男' union all
select '李四','男' union all
select '李四','女' union all
select '王五','男'
go

update test
set name=sex+name where exists(select 1 from test a 
where a.name=test.name and a.sex<>test.sex)

select * from test

/*
name sex
--------------------------
张三 男
男李四 男
女李四 女
王五 男
*/

------解决方案--------------------
引用:
只修改男的 男的姓名前加男就可以了 女生不用修改



if OBJECT_ID('test') is not null
drop table test
go
create table test
(
name varchar(20),
sex varchar(2)
)
go
insert test
select '张三','男' union all
select '李四','男' union all
select '李四','女' union all
select '王五','男'
go

update test
set name=sex+name where exists(select 1 from test a 
where a.name=test.name and a.sex<>test.sex) and sex='男'

select * from test

/*
name sex
-------------------------
张三 男
男李四 男
李四 女
王五 男
*/


都写成这样了  还在问  貌似你完全不懂  或者就是懒