【问题标题】:Update only in case where its not null [duplicate]仅在其不为空的情况下更新 [重复]
【发布时间】:2017-02-05 02:31:38
【问题描述】:

我想使用从 table2 中选择更新 table1 中的列 (a),但仅在 table2 中现有列 (b) 不为空时进行更新。我不想使用 where 语句(不为空),因为它会影响我的所有代码。

我的代码示例:

update table1 set column(a) = (select column(b) from table2)

我正在尝试这样的事情

update table1 set column(a) = not null(select column(b) from table2)

例子:

update ExpressMarketCheck set Barcode = (select barcode from ExpressMarket), Name=(select name from expressmarket), price=(select price from expressmarket)

【问题讨论】:

  • “影响我所有的代码”是什么意思?为什么可以点头使用简单、标准的WHERE 子句?
  • WHERE 子句有什么问题?

标签: sql sql-server


【解决方案1】:

您可以使用此查询来实现您的约束。

Update ExpressMarketCheck set Barcode = (select barcode from ExpressMarket where barcode IS NOT NULL)

【讨论】:

  • 不相关子查询?
  • 这正是他不想要的。 “我不想使用 where 语句(不为空)”
  • 感谢您的信息。最初,当我发布答案时,该约束不存在。
  • 没关系,我只需对我的代码稍作改动就可以了!谢谢!
【解决方案2】:

mySQL 有一个IFNULL 函数,所以你可以这样做:

UPDATE your_table_name
SET your_column_name= "data",
scan_created_date = ISNULL( your_column_name, "data" )
WHERE id = X

【讨论】:

  • 在 SQL-Server 中你可以使用ISNULL()
  • 感谢指点-:)
【解决方案3】:

我想你想要一个join:

update emc
    set Barcode = em.barcode, 
        Name = em.name,
        price= em.price
     from ExpressMarketCheck emc join
          expressmarket em
          on emc.?? = em.??;

我无法从您的问题中得知join 应使用哪些列。 ?? 是占位符。

【讨论】:

    猜你喜欢
    • 2014-01-10
    • 2020-05-04
    • 2016-05-25
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多