2

我想更新表格,但不知道要更新哪一列。这是一个简单的例子

Update tablename
set 
if col1='A' then col1='IA',col2='XXX'
if col3='A' then col3='IA', col4='XXX'
where
(col1='A' and col2='UNKNOWN') or (col3='A' and col4='UNKNOWN')

这里要么col1col3肯定包含'A'

请给我正确的查询

4

2 回答 2

5

试试这个查询

UPDATE tablename
SET col1=(CASE WHEN col1 LIKE 'A' THEN col1='IA' ELSE col1 END),
col2=(CASE WHEN col1 LIKE 'A' THEN col2='XXX' ELSE col2 END),
col3=(CASE WHEN col3 LIKE 'A' THEN col3='IA' ELSE col3 END),
col3=(CASE WEHN col3 LIKE 'A' THEN col4='XXX' ELSE col4 END)
WHERE
(col1='A' AND col2='UNKNOWN') OR (col3='A' AND col4='UNKNOWN')
于 2013-01-29T10:44:54.490 回答
0

和旧帖子,但对于那些偶然发现这一点的谷歌人。这行不通。例如

THEN col1='IA'

将返回一个布尔值并将 col1 设置为1ie true 而不是只返回'IA'withTHEN 'IA' ELSE col1 END

为了清楚起见:

UPDATE tablename
SET col1=(CASE WHEN col1 LIKE 'A' THEN 'IA' ELSE col1 END),
col2=(CASE WHEN col1 LIKE 'A' THEN 'XXX' ELSE col2 END),
col3=(CASE WHEN col3 LIKE 'A' THEN 'IA' ELSE col3 END),
col3=(CASE WEHN col3 LIKE 'A' THEN 'XXX' ELSE col4 END)
WHERE
(col1='A' AND col2='UNKNOWN') OR (col3='A' AND col4='UNKNOWN')
于 2020-08-04T04:38:20.437 回答