急问:请教精通SQL的姐妹们

B
BabyEmily
楼主 (北美华人网)
请问大家,工作上有个project需要,5K的 records, 将它们的一个值修改。update table name set column1='codeA' +'codeB' where column1 is null;其中codeA是primary key(有5K的变量值), codeB是不变的常值。请问怎样写这个command,可以批处理所有的5K数据,每一条出来的值都是它所对应的primary key加上不变的值的combination,而不需要一条条每一个primary key写一个命令。很感谢!我的SQL还不精通,特来请教,向大家虚心学习。
y
youyichanzi
请问大家,工作上有个project需要,5K的 records, 将它们的一个值修改。update table name set column1=''codeA'' +''codeB'' where column1 is null;其中codeA是primary key(有5K的变量值), codeB是不变的常值。请问怎样写这个command,可以批处理所有的5K数据,每一条出来的值都是它所对应的primary key加上不变的值的combination,而不需要一条条每一个primary key写一个命令。很感谢!我的SQL还不精通,特来请教,向大家虚心学习。

BabyEmily 发表于 2021-04-25 10:42

codeA用列名,别加引号啊。对自己的命令不确定的时候先用select试一下
p
pop
这还需要精通SQL吗?哪怕是初学SQL也应该会啊。

d
deardeerlulu
听二楼的,run update之前一定先select
d
deardeerlulu
加一句,看看几个column数据类型,需不需要一下data type conversion
B
BabyEmily
感谢二楼和上面最后一楼的姐妹!我早上赶着要出门,没有把要问的说清楚,抱歉。下面是我重新修改过的,再次来请教大家。 set新值给column A为总共几千条数据,每一条数据的column A新值是combine固定值ZZZ +那条数据的primary key. 如果按下面的写法,得逐条record写一个SET命令(因为primary key不同)。想请教的是,怎么改写下面的命令,可以用一条set的命令,或更简洁的命令,同时为几千条数据定新column A的值。非常感谢!!
update table X set column A ='ZZZ_111' where column A is null and primary_key ='111'; set column A ='ZZZ_222' where column A is null and primary_key ='222'; set column A ='ZZZ_333' where column A is null and primary_key ='333'; --ZZZ is a fixed value. --111, 222, 333 and so on。。。are the primary keys associated with each row of values.  

今生无悔
update table X set column A =concat(''zzz_'', primary_key) where column A is null;
M
MochaLatte
something like update tableA set columnA = (select primaryKey from tableA ) || ' ' || constantValeStr where columnA is null check the systax from your db vendor
B
BabyEmily
感谢楼上的两位回复,我明天都试试!
m
miaoerl
听大家的,每次update之前,一定要select出来看看是不是你update要的结果,如果是再写update语句,对新人尤其重要……
s
sindylee
先select确保无误再update啊
c
cloudymind
听大家的,每次update之前,一定要select出来看看是不是你update要的结果,如果是再写update语句,对新人尤其重要……
miaoerl 发表于 2021-04-25 23:39

👍加一条back up the entire database.