具有多个条件的SQL更新表在where语句中访问ACCESS

时间:2017-05-16 14:12:02

标签: sql ms-access

我正在尝试更新表格中的多个列。这是我正在尝试做的一个例子:

说我有这张桌子:

User | Letter1 | Letter2 
 1   | 'A'     |  'X'
 2   | 'X'     |  'X'
 3   | 'R'     |  'G'

我想把它转换成这样的表格,其中所有的X都被“_”(下划线)取代。

User | Letter1 | Letter2 
 1   | 'A'     |  '_'
 2   | '_'     |  '_'
 3   | 'R'     |  'G'

我试过了:

Update table1 
SET Letter1 = '_', Letter2 = '_'
WHERE Letter1 = "X", Letter2 = "X"

然而,这不起作用。任何帮助都会很棒。 提前致谢

2 个答案:

答案 0 :(得分:4)

如果您想在单次更新中执行此操作,则需要条件逻辑:

Update table1 
    SET Letter1 = IIF(Letter1 = "X", "_", Letter1),
        Letter2 = IIF(Letter1 = "X", "_", Letter2)
    WHERE Letter1 = "X" OR Letter2 = "X";

然而,两个单独的陈述可​​能更清楚:

Update table1 
    SET Letter1 = "_"
    WHERE Letter1 = "X";

Update table1 
    SET Letter2 = "_"
    WHERE Letter2 = "X";

答案 1 :(得分:1)

使用以下查询

Update table1 
SET Letter1 = '_', Letter2 = '_'
WHERE Letter1 = 'X' or Letter2 =  'X';

或使用两个更新语句

Update table1 
SET Letter1 = '_'
WHERE Letter1 = 'X';

Update table1 
SET  Letter2 = '_'
WHERE  Letter2 =  'X';