微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql中的concat列选择

我尝试了一些变化,但是从我对 the documentation的阅读中,这个模式应该可以正常工作”|| val1 || val1 …但我的结果是一个空列……
thedb=# \d buildings_propertyvalue;
                              Table "public.buildings_propertyvalue"
  Column   |          Type          |                              Modifiers                               
-----------+------------------------+----------------------------------------------------------------------
 id        | integer                | not null default nextval('buildings_propertyvalue_id_seq'::regclass)
 prop_id   | integer                | not null
 place_id  | integer                | not null
 float_val | double precision       | 
 int_val   | integer                | 
 char_val  | character varying(255) | 
 text_val  | text                   | 

thedb=# select * from buildings_propertyvalue limit 10;
 id  | prop_id | place_id | float_val | int_val | char_val | text_val 
-----+---------+----------+-----------+---------+----------+----------
 798 |       3 |      170 |           |     831 |          | 
   2 |      46 |      180 |           |       0 |          | 
 733 |       2 |      180 |        40 |         |          | 
 737 |      10 |      180 |           |       0 |          | 
 740 |       5 |      345 |       100 |         |          | 
 742 |      10 |      345 |           |       0 |          | 
  11 |       2 |      170 |        50 |         |          | 
 744 |      11 |      345 |         0 |         |          | 
 746 |      14 |      345 |           |         | 52       | 
 749 |      46 |      348 |           |       0 |          | 
(10 rows)

thedb=# select prop_id,place_id,'' || float_val || int_val || char_val || text_val as val from buildings_propertyvalue limit 10;
 prop_id | place_id | val 
---------+----------+-----
       3 |      170 | 
      46 |      180 | 
       2 |      180 | 
      10 |      180 | 
       5 |      345 | 
      10 |      345 | 
       2 |      170 | 
      11 |      345 | 
      14 |      345 | 
      46 |      348 | 
(10 rows)
将NULL与非空字符串连接会产生NULL

由于您的* _val列可以为空,因此可能正在发生的事情.

试试这个:

'' || COALESCE(float_val::TEXT,'') || COALESCE(int_val::TEXT,'') || COALESCE(char_val,'') || COALESCE(text_val,'')

或者,如果您最多只能有一个非空值,只需:

COALESCE(float_val::TEXT,int_val::TEXT,char_val,text_val,'')

请注意,在Postgresql中,与其他一些引擎不同,TEXT has no downsides compared to VARCHAR.分离TEXT和VARCHAR数据毫无意义.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐