[英]Find data from multiple tables in a single query
I'm working in postgreSQL and my proble is this: I have 3 tables with a different ID serialized. 我在postgreSQL中工作,我的问题是:我有3个表,序列化了不同的ID。 For example, if I insert a row in the table 1, the id will be '00001', if I insert a row in the table 3. the id will be '00002'.... 例如,如果我在表1中插入一行,则ID为'00001',如果我在表3中插入一行,则ID为'00002'....
So, i'm trying to create a query that gets the data with from the three tables using a single query. 因此,我试图创建一个查询,使用一个查询从三个表中获取数据。 I don't know if is necessary create a function plpgsql or just with a simple query, I did not make it yet. 我不知道是否有必要创建一个函数plpgsql或仅使用一个简单的查询,但是我还没有做到。
I want something like this: (But in postgreSQL) 我想要这样的东西:(但是在postgreSQL中)
function getData(ID){
if( ID exists in table 1){
return select * from table 1
}
if( ID exists in table 2){
return select * from table 1
}
if( ID exists in table 3){
return select * from table 3
}
}
But of course, this code is in another language and wrong, a pseudocode. 但是,当然,此代码是另一种语言,错误的是伪代码。
Any suggestion? 有什么建议吗?
Why not just use union all
? 为什么不只使用union all
?
select t1.* from table1 t1 where t1.id = 1
union all
select t2.* from table2 t2 where t2.id = 1
union all
select t3.* from table3 t3 where t3.id = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.