Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails中postgres查询的空或空字段检查_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails rails中postgres查询的空或空字段检查

Ruby on rails rails中postgres查询的空或空字段检查,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我试图在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位于或接近“)”第1行

我试图在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位于或接近“)”第1行:…HEN (items.item_代码为NULL或items.item_代码=)然后单击item


您对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

“空或单引号”,您是指
NULL
还是空字符串?因为单引号是一个完全不同的东西,“它没有执行”;错误是什么?错误类似于PG::error:error:syntax error at or near“)”第1行:…HEN(items.item_code为NULL或items.item_code=)然后是…您可能给php语句加了单引号-对吧?然后是ruby中的引号-在我的回答中检查sql第二个很好,这就是引号的问题,谢谢
CASE WHEN (items.item_code is NULL OR items.item_code = \'\') THEN
items.name::text ELSE items.item_code::text END as item_code