如何在postgresql中将行拆分为多行

时间:2016-02-20 17:46:11

标签: sql regex postgresql

我有一个包含两列的表:ID和汽车。 示例数据如下所示:

ID  |  Cars
-----------------
1   |  opel, honda and land rover
2   |  ford and porshe, damaged
3   |  volkswagen
4   |  opel, seat, damaged

我想把它分成:

ID  |  Cars
-----------------
1   |  opel
1   |  honda
1   |  land rover
2   |  ford
2   |  porshe, damaged
3   |  volkswagen
4   |  opel
4   |  seat, damaged

因此分隔符为and,,但不是, damaged 如何使用正则表达式在postgresql中拆分它?

修改

如何使其适用于

等记录
5 | land rover and opel, and ford
6 | ford; mazda and toyota

1 个答案:

答案 0 :(得分:4)

您可以使用regexp_split_to_table带有负面预测功能进行损坏;

SELECT "ID", regexp_split_to_table("Cars", '((, (?!damaged))| and )') "Cars" 
FROM mytable;

 ID |      Cars
----+-----------------
  1 | opel
  1 | honda
  1 | land rover
  2 | ford
  2 | porshe, damaged
  3 | volkswagen
  4 | opel
  4 | seat, damaged
(8 rows)

An SQLfiddle to test with

编辑:对于你的新例子,正则表达式必须稍微调整一下;

SELECT "ID", regexp_split_to_table("Cars", '(([,;] (?!damaged))|[,;]? and )') "Cars" 
FROM mytable;

Another SQLfiddle