暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL数据库连接字符串常用方法

一、使用 || 运算符

postgres=# SELECT 'Hello,' || 'World!' AS connect_word;
+--------------+
| connect_word |
+--------------+
| Hello,World! |
+--------------+
(1 row)

二、使用 concat_ws() 函数

在拼接的字符串之间插入一个特定的分隔符,可以使用 concat_ws() 函数。第一个参数是分隔符,之后是需要拼接的字符串。

postgres=# SELECT concat_ws(' ', 'Hello', 'World') AS connect_word;
+--------------+
| connect_word |
+--------------+
| Hello World  |
+--------------+
(1 row)

三、使用 concat() 函数

concat() 函数支持多字符串连接。

postgres=# SELECT concat('Hello, ', 'World!','xxx',' heihei') AS connect_word;
+-------------------------+
|      connect_word       |
+-------------------------+
| Hello, World!xxx heihei |
+-------------------------+
(1 row)

四、使用 format() 函数

format() 函数可以用来格式化字符串,并且在其中嵌入变量。

postgres=# SELECT format('Hello,%s!', 'World') AS connect_word;
+--------------+
| connect_word |
+--------------+
| Hello,World! |
+--------------+
(1 row)

五、使用 string_agg() 函数

将一列中的多个值拼接到一个字符串中时,可以使用string_agg。

postgres=# SELECT string_agg(datname, ',') AS connect_word FROM pg_database;
+-------------------------------------------+
|               connect_word                |
+-------------------------------------------+
| template0,template1,postgres,test_upgrade |
+-------------------------------------------+
(1 row)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论