postgresql - 从Postgresql中的列中的多个单词的条目中提取单个单词

标签 postgresql

我有一个包含多列的表,其中有一列名为类别。列中的每个条目都由多个单词组成。每行都有不同数量的单词。如何创建另一个包含一列的表,其中每个条目都是前一个表的类别列中的单个词?

我拥有的表格和我想要定位的列:

  • 类别
    1. 高尔夫,积极的生活
    2. 特色食品、餐厅、点心、进口食品、食品、中国食品、民族食品、海鲜

我想要一个只有一列的表格:

  • 类别
    1. 高尔夫
    2. 积极的生活
    3. 特色食品
    4. 餐馆
    5. 点心
    6. ...

编辑:对于 future 的任何人,我都需要一张永久性的 table 。查看评论以获得完整答案。

最佳答案

OK,在PG中很容易实现,只需要使用string_to_arrayunnest函数,如下:

select
    unnest(string_to_array(categories,',')) as word
into my_new_table
from
    table

示例如下:

select unnest(string_to_array('Specialty Food, Restaurants, Dim Sum, Imported Food, Food, Chinese, Ethnic Food',','));
     unnest     
----------------
 Specialty Food
  Restaurants
  Dim Sum
  Imported Food
  Food
  Chinese
  Ethnic Food
(7 rows)

希望它对你有用。

关于postgresql - 从Postgresql中的列中的多个单词的条目中提取单个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56356983/

相关文章:

sql - pg_ctl : no database directory specified and environment variable PGDATA unset

php - 为什么我无法使用 codeigniter 连接到 postgresql?

arrays - 取消引用 postgres 二维数组

sql - Postgres : How do I retrieve data from child table with many-to-many relations?

postgresql - postgresql 中 varbinary(10) 的等价物是什么

sql - 添加缺失行的默认值(学生和科目的组合)

java - 如何将 postgresql 添加到 OpenShift Spring 应用程序?

javascript - 从node.js中的PostgreSQL查询返回查询结果状态

java - 如何在 Hibernate 中处理命名 native 查询中的额外字段

postgresql - 在连接时创建 PostgreSQL 数据库