SQL查询结果中的重复行 [英] Duplicate (repeat) rows in sql query result
本文介绍了SQL查询结果中的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个包含两行的表
Let's say I have a table with two rows
id | value |
----+-------+
1 | 2 |
2 | 3 |
我想编写一个查询,该查询将根据值重复(重复)每一行。
我想要这个结果(总共5行):
I want to write a query that will duplicate (repeat) each row based on the value.
I want this result (5 rows total):
id | value |
----+-------+
1 | 2 |
1 | 2 |
2 | 3 |
2 | 3 |
2 | 3 |
我正在使用PostgreSQL 9.4。
I'm using PostgreSQL 9.4.
推荐答案
您可以使用 generate_series()
:
select t.id, t.value
from (select t.id, t.value, generate_series(1, t.value)
from t
) t;
您可以使用侧向联接执行相同的操作:
You can do the same thing with a lateral join:
select t.id, t.value
from t, lateral
generate_series(1, t.value);
这篇关于SQL查询结果中的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文