邮政系统 |如何将两个或多个 table 组合成单个 table
Postgres | how to combine from two or more table into single table
我有很多数据被分成两个或更多 tables.Then 如何从所有 table 中获取数据。
首先table点table分为三部分
first table = ab_p1_point
second table = ab_p2_point
third table = ab_p3_point
另一个table是第table行,它又分为三个部分
first table = bc_p1_line
second table = bc_p2_line
third table = bc_p3_line
如何将点 tables 的所有部分组合成单个点 table 并将线 tables 的所有部分组合成单线 table 然后执行操作在 table 之间。
假设所有三个表都具有相同的结构,您可能会使用 union all
:
select * from (
select f1, f2, f3 from ab_p1_point
union all
select f1, f2, f3 from ab_p2_point
union all
select f1, f2, f3 from ab_p3_point
) t
where f1 = 'abc'
您可以通过将查询放入视图中来使其更易于使用。
但是为什么要在多个表中拆分行?
您可以使用 CTE(WITH
语句):
WITH points AS (
SELECT * FROM ab_p1_point
UNION
SELECT * FROM ab_p2_point
UNION
SELECT * FROM ab_p3_point
)
, lines AS (
SELECT * FROM bc_p1_line
UNION
SELECT * FROM bc_p2_line
UNION
SELECT * FROM bc_p3_line
)
SELECT *
FROM points
INNER JOIN lines
ON points.column = lines.column
我有很多数据被分成两个或更多 tables.Then 如何从所有 table 中获取数据。
首先table点table分为三部分
first table = ab_p1_point
second table = ab_p2_point
third table = ab_p3_point
另一个table是第table行,它又分为三个部分
first table = bc_p1_line
second table = bc_p2_line
third table = bc_p3_line
如何将点 tables 的所有部分组合成单个点 table 并将线 tables 的所有部分组合成单线 table 然后执行操作在 table 之间。
假设所有三个表都具有相同的结构,您可能会使用 union all
:
select * from (
select f1, f2, f3 from ab_p1_point
union all
select f1, f2, f3 from ab_p2_point
union all
select f1, f2, f3 from ab_p3_point
) t
where f1 = 'abc'
您可以通过将查询放入视图中来使其更易于使用。
但是为什么要在多个表中拆分行?
您可以使用 CTE(WITH
语句):
WITH points AS (
SELECT * FROM ab_p1_point
UNION
SELECT * FROM ab_p2_point
UNION
SELECT * FROM ab_p3_point
)
, lines AS (
SELECT * FROM bc_p1_line
UNION
SELECT * FROM bc_p2_line
UNION
SELECT * FROM bc_p3_line
)
SELECT *
FROM points
INNER JOIN lines
ON points.column = lines.column