我有一个名为sinvent的表,其中包含accno,jno和saccno列。 jno和saccno已经被数字填充,很多数字重复。我需要根据以下值填充accno,这些值基于与它关联的jno,但有些范围重叠。例如,我首先需要用'4010'填充整个accno列,然后以下条件确定了用我在下面的代码中尝试过替换的值,并且它不执行所有任务,仅执行其中一些任务。如何编写单个查询来执行此操作?

UPDATE sinvent
accno = 4010 all
accno = CASE
  WHEN jno < '09999' THEN '4010'
  WHEN jno = '00011' THEN '4011'
  WHEN jno = '00012' THEN '4012'
  WHEN jno = '00014' THEN '4714'
  WHEN (jno > '80000' AND jno < '99998') THEN '4018'
  WHEN (saccno = '7' OR saccno = '8') THEN '4008'
  WHEN (jno = '4714' OR jno = '04714') THEN '4714'
  WHEN (jno = '4012' OR jno = '04012') THEN '4012'
  WHEN (jno = '4006' OR jno = '4506') THEN '4006'
  WHEN jno = '4116' THEN '4116'
  WHEN jno = '04011' THEN '4011'
  WHEN (jno > '60000' AND jno < '80000') THEN '4015'
  END

最佳答案

这工作

UPDATE sinvent
accno = case
when (jno > '60000' AND jno < '80000') then '4015'
when jno = '04011' then '4011'
when jno = '4116' then '4116'
when jno = '4506' then '4006'
when jno = '4006' then '4006'
when jno = '4012' then '4012'
when jno = '04012' then '4012'
when jno = '4714' then '4714'
when jno = '04714' then '4714'
when (saccno = '7' OR saccno = '8') THEN '4008'
when jno = '00014' then '4714'
when jno = '00012' then '4012'
when jno = '00011' then '4011'
when (jno > '80000' AND jno < '99998') then '4018'
when jno < '09999' then '4010'
when code = '1168' then '4014'
else accno
end

关于mysql - 如何使用多个条件填充表的字段,其中一些条件重叠,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30786205/

10-09 07:06