我正在尝试在 posgtres 查询中使用 case 检查两个条件,在我的表中,一个字段可能有两个值要么为空要么为单引号 ('') 所以我尝试了这个查询但它没有执行
CASE WHEN (items.item_code is NULL OR items.item_code = '') THEN
items.name::text ELSE items.item_code::text END as item_code
错误如:
PG::Error: ERROR: syntax error at or near ")" LINE 1: ...HEN (items.item_code is NULL OR items.item_code = ) THEN ite..
最佳答案
你在 ruby 中的引号有问题,在 sql 语句中没有。 尝试更改为:
CASE
WHEN (coalesce(length(items.item_code),0) + length(items.item_code)) < 1
THEN items.name::text ELSE items.item_code::text
END as item_code
上面的代码与您的代码进行相同的条件检查,但使用引号进行了空洞。我想你应该是这样的:
CASE WHEN (items.item_code is NULL OR items.item_code = \'\') THEN
items.name::text ELSE items.item_code::text END as item_code
关于ruby-on-rails - 在 rails 中使用 postgres 查询检查空字段或空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41159560/