Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-如何从多个表中获取值而不产生歧义_Sql_Postgresql - Fatal编程技术网

SQL-如何从多个表中获取值而不产生歧义

SQL-如何从多个表中获取值而不产生歧义,sql,postgresql,Sql,Postgresql,如果以前有人问过这个问题(可能是这样),我会道歉。我以前从未使用过SQL,我得到的答案只会让我更加困惑 我需要找出不同表上是否存在ID,并从所有表中获取总数。 我的问题是: select * from public.ui1, public.ui2, public.ui3 where id = '123' 所以,如果ui1和ui2中不存在id 123,但ui3中确实存在id 123,我仍然希望得到它。(如果它存在于其他表格中,我显然希望得到它) 我目前收到一条不明确的错误消息,因为所有表中都存在

如果以前有人问过这个问题(可能是这样),我会道歉。我以前从未使用过SQL,我得到的答案只会让我更加困惑

我需要找出不同表上是否存在ID,并从所有表中获取总数。

我的问题是:

select * from public.ui1, public.ui2, public.ui3 where id = '123'
所以,如果ui1和ui2中不存在id 123,但ui3中确实存在id 123,我仍然希望得到它。(如果它存在于其他表格中,我显然希望得到它)

我目前收到一条不明确的错误消息,因为所有表中都存在id,但我不确定如何以适当的方式构造此查询。我试图加入,但惨败了。任何关于如何重建它的帮助和愚蠢的证明解释都将不胜感激


编辑:我最后想知道的是,在任何一个表中是否存在id=123。

有点不清楚您期望的结果是什么。如果需要计数,则可以使用
UNION ALL

select 'ui1' as source_table, 
       count(*) as num_rows
from public.ui1
where id = 123
union all
select 'ui2', 
       count(*)
from public.ui2
where id = 123
union all
select 'ui3', 
       count(*)
from public.ui3
where id = 123
如果您只想知道id是否存在于至少一个表中(因此为真/假),则可以使用:

select exists (select id from ui1 where id = 123
               union all 
               select id from ui2 where id = 123
               union all
               select id from ui3 where id = 123)

有点不清楚你期望的结果是什么。如果需要计数,则可以使用
UNION ALL

select 'ui1' as source_table, 
       count(*) as num_rows
from public.ui1
where id = 123
union all
select 'ui2', 
       count(*)
from public.ui2
where id = 123
union all
select 'ui3', 
       count(*)
from public.ui3
where id = 123
如果您只想知道id是否存在于至少一个表中(因此为真/假),则可以使用:

select exists (select id from ui1 where id = 123
               union all 
               select id from ui2 where id = 123
               union all
               select id from ui3 where id = 123)
我最后想知道的是,任何一个表中是否存在id=123

最好的方法可能就是使用
exists

select v.id, 
       (exists (select 1 from public.ui1 t where t.id = v.id) or
        exists (select 1 from public.ui2 t where t.id = v.id) or
        exists (select 1 from public.ui3 t where t.id = v.id)
       ) as exists_flag
from (values (123)) v(id);
如前所述,这将为
values()
中定义的每个
id
返回一行,以及标识该id是否存在的标志——您正在询问的问题

如果您需要其他信息,例如id存在于哪些表中,或者每个表出现的次数,则可以轻松调整此参数

我最后想知道的是,任何一个表中是否存在id=123

最好的方法可能就是使用
exists

select v.id, 
       (exists (select 1 from public.ui1 t where t.id = v.id) or
        exists (select 1 from public.ui2 t where t.id = v.id) or
        exists (select 1 from public.ui3 t where t.id = v.id)
       ) as exists_flag
from (values (123)) v(id);
如前所述,这将为
values()
中定义的每个
id
返回一行,以及标识该id是否存在的标志——您正在询问的问题


如果您需要其他信息,例如id存在于哪些表中,或者每个表出现的次数,可以很容易地对其进行调整。

@jarlh:实际上我认为
UNION
是答案。@a_horse_没有名字,您可能是对的。@jarlh:实际上我认为
UNION
是答案。@a_horse_没有名字,你可能是对的。非常感谢你的帮助。查询似乎忽略了where id='123'条件。我在结尾添加了它,但仍然得到相同的数字(总数,未过滤)@PloniStacker:对不起,我编辑了代码。你需要为每一个选择的工会申请。太好了,非常感谢你!非常感谢你的帮助。查询似乎忽略了where id='123'条件。我在结尾添加了它,但仍然得到相同的数字(总数,未过滤)@PloniStacker:对不起,我编辑了代码。你需要为每一个选择的工会申请。太好了,非常感谢你!