【问题标题】:How can I skip the then part of a case statement in SQL如何在 SQL 中跳过 case 语句的 then 部分
【发布时间】:2019-10-02 03:07:39
【问题描述】:

我有一种情况,如果在 when 部分满足这些条件,我什么都不需要做,否则就赋值。我在我的 SQL 中使用case when <expression> then <some operation/assignment> end

我想跳过这个 then 部分,因为这是一个特殊条件,我不希望这里有任何分配,只是传递给 else

(case
    when '$usertype_id' = 'null' or '$usertype_id' = 0 or '$usertype_id' = 999 
       then //do nothing
    else x.usertype_id = '$usertype_id'
end)

请让我知道如何实现这一目标。谢谢。

【问题讨论】:

  • 需要帮助的东西:SQL 的风格。更多的查询。有效的语法(看起来你在代码中随机穿插撇号)
  • @dougp 抱歉,我修复了这些问题。谢谢
  • 无论如何你都不能用case做作业。
  • 您确定要检查 varchar 文本“null”,而不是“is null”检查吗?

标签: sql


【解决方案1】:

你没有。你使用条件逻辑没有 case expression:

('$usertype_id = 'null' or '$usertype_id = 0 or '$usertype_id = 999) or
(x.usertype_id = '$usertype_id')

更重要的是,我强烈建议您使用参数,而不是使用参数值修改查询字符串。

【讨论】:

  • 您能否详细说明“强烈鼓励您使用参数而不是使用参数值修改查询字符串”的意思。谢谢
  • @HarshalJani 。 . .您似乎将变量直接放入查询字符串中。您应该改用参数。确切的语法取决于您的数据库和应用程序层,但有很多示例可以向您展示。
猜你喜欢
  • 2017-08-22
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多