【问题标题】:Update table if any value =如果有任何值,则更新表 =
【发布时间】:2019-07-23 03:41:34
【问题描述】:

如果 table1 中的任何值 = table2 中的值,我正在尝试更新我的表

Table1 作为 1 列包含数据 Table2 与数据一样多的列

如果 table2(data) = table1(data) 更新

但不工作

如果我将 table2 设置为 1 列,我有一个代码可以工作

这个正在工作,但 table2 只需要 1 列

    UPDATE table1
    SET column1 = 'correct'
    WHERE column2 in (SELECT column1 from table2);

我希望能够拥有更多的专栏

可能是这样的:

    UPDATE
    SET column1 = 'correct'
    WHERE column2 in (SELECT * from table2);

错误:

结果:子选择返回 11 列 - 预期为 1

我该怎么做?

【问题讨论】:

    标签: sqlite


    【解决方案1】:

    也许你可以用 EXISTS 做到这一点:

    UPDATE table1
    SET column1 = 'correct'
    WHERE EXISTS (
      SELECT 1 FROM table2
      WHERE table1.somecolumn = table2.someothercolumn
    );
    

    如果您想检查table1.someothercolumntable2 的多列:

    UPDATE table1
    SET column1 = 'correct'
    WHERE EXISTS (
      SELECT 1 FROM table2
      WHERE table1.somecolumn in (table2.col1, table2.col2, ...)
    );
    

    【讨论】:

    • 有没有一种方法可以让我获得所有列而不需要从 table2 中指定每一列?在这部分...WHERE table1.somecolumn in (table2.col1, table2.col2, ...)...
    • 不,你不能用 sql。
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 2022-01-26
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2018-04-12
    • 2012-10-29
    相关资源
    最近更新 更多