求助!SQL写法问题,把筛选的结果一次性修改

伟大de虫子 2012-06-07 11:42:09
在TF_BOM表, PRD_NO这个字段:

字段 PRD_NO
---------------------
abc
abcR
cde
cdeR
def
defR
---------------------

下面语句是我筛选出结果的SQL:
----------------------------------
select distinct PRD_NO from TF_BOM where PRD_NO in(select left(PRD_NO,len(PRD_NO) -1) from TF_BOM where right(PRD_NO,1) ='R')

我想把结果一次性修改成带R的,求教怎么写SQL,可以用多行代码(只要能在查询器里执行就可以)
...全文
135 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
伟大de虫子 2012-06-07
  • 打赏
  • 举报
回复

嗯。立即去试。 还是CSDN牛B。

回头给分
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test]([PRD_NO] varchar(4))
insert [test]
select 'abc' union all
select 'abcR' union all
select 'cde' union all
select 'cdeR' union all
select 'tyt' union all
select 'tytR' union all
select 'gfh' union all
select 'fgg' union all
select 'def' union all
select 'defR'


update test
set [PRD_NO]=[PRD_NO]+'R'
where exists(select 1 from test b where test.PRD_NO=left(b.PRD_NO,LEN(b.PRD_NO)-1))

select * from test
/*
PRD_NO
-----------
abcR
abcR
cdeR
cdeR
tytR
tytR
gfh
fgg
defR
defR
*/

--你看看这个结果对不?
伟大de虫子 2012-06-07
  • 打赏
  • 举报
回复

要把结果中的字段PRD_NO,update成带“R”的内容。
伟大de虫子 2012-06-07
  • 打赏
  • 举报
回复

不能简明一点吗。 就给我要用的代码,你加上你的测试代码,会让我很困惑。。。新手啊
伟大de虫子 2012-06-07
  • 打赏
  • 举报
回复

大侠。。。我看不懂啊。 你里面都没有修改的语句啊。。。

只是把结果选了出来吗?
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

谢谢,我试试

但是你里面用的 表test....是为了测试用是不是?

我实际要使用的时候,是不是要改成我的表名?
[/Quote]

必须的啊
伟大de虫子 2012-06-07
  • 打赏
  • 举报
回复
谢谢,我试试

但是你里面用的 表test....是为了测试用是不是?

我实际要使用的时候,是不是要改成我的表名?
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test]([PRD_NO] varchar(4))
insert [test]
select 'abc' union all
select 'abcR' union all
select 'cde' union all
select 'cdeR' union all
select 'tyt' union all
select 'tytR' union all
select 'gfh' union all
select 'fgg' union all
select 'def' union all
select 'defR'


select [PRD_NO]+'R' as [PRD_NO]
from test a
where exists(select 1 from test b where a.PRD_NO=left(b.PRD_NO,LEN(b.PRD_NO)-1))
/*
PRD_NO
-----------
abcR
cdeR
tytR
defR
*/
伟大de虫子 2012-06-07
  • 打赏
  • 举报
回复

上面写错了。是剔除带R的,剩下不带R的记录.....
伟大de虫子 2012-06-07
  • 打赏
  • 举报
回复

我详细说给你听吧。不好意思表达得不好:

------------------------

PRO_NO里的记录,有大概1500条记录,是成对的,比如abc就对应有一条记录是abcR,def就对应有一条记录是defR,其他的类似。

现在我想要做的是:把这1500条成对对应的记录(abc,abcR;def,defR....),筛选出来,然后再剔除不带R的,就剩下不带R的那一半记录,把这些不带R的,加上R字符。

比如abc,变成abcR.....
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([PRD_NO] varchar(4))
insert [test]
select 'abc' union all
select 'abcR' union all
select 'cde' union all
select 'cdeR' union all
select 'def' union all
select 'defR'

update test
set [PRD_NO]=[PRD_NO]+'R'
where CHARINDEX('R',[PRD_NO])=0

select * from test

/*
PRD_NO
-------------------
abcR
abcR
cdeR
cdeR
defR
defR
*/


--真心不明白你说的是什么意思。给出测试数据最好给出自己期待的结果
伟大de虫子 2012-06-07
  • 打赏
  • 举报
回复

就是一次性把不带R的记录,修改面带R的。
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([PRD_NO] varchar(4))
insert [test]
select 'abc' union all
select 'abcR' union all
select 'cde' union all
select 'cdeR' union all
select 'def' union all
select 'defR'

select case when right([PRD_NO],1)='R' then LEFT([PRD_NO],LEN([PRD_NO])-1)
else [PRD_NO] end as [PRD_NO] from test

/*
PRD_NO
---------------
abc
abc
cde
cde
def
def
*/
  • 打赏
  • 举报
回复
没明白你的意思。能不能给出你期待的结果
zhazhuzhao 2012-06-07
  • 打赏
  • 举报
回复
呵呵,感觉楼上的还是有点不严谨啊!
update test
set [PRD_NO]=[PRD_NO]+'R'
where exists(select 1 from test b where test.PRD_NO=left(b.PRD_NO,LEN(b.PRD_NO)-1) and right(b.PRD_NO,1)='R' )
似乎缺了判断为R的情形,也就是说 abc和abcd也符合条件了!

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧