gpt4 book ai didi

sql - 如何检查我所有表的存在?在 postgresql 中

转载 作者:行者123 更新时间:2023-11-29 12:18:55 25 4
gpt4 key购买 nike

我想检查我所有的表是否存在。如果所有表都存在则发送 1,如果缺少一张或多张表则发送 0。

select 1 from information_schema.tables tbl where tbl.table_name='graphe';
select 1 from information_schema.tables tbl where tbl.table_name='proprieteindicateur';
select 1 from information_schema.tables tbl where tbl.table_name='graphe_proprieteindicateur';
select 1 from information_schema.tables tbl where tbl.table_name='utilisateurs';
select 1 from information_schema.tables tbl where tbl.table_name='profil';
select 1 from information_schema.tables tbl where tbl.table_name='droit';

最佳答案

这可以用一条语句完成:

with table_list (name) as (
values
('graphe'),
('proprieteindicateur'),
('graphe_proprieteindicateur'),
('utilisateurs'),
('profil'),
('droit')
)
select count(*) = (select count(*) from table_list)
from information_schema.tables it
join table_list l on it.table_name = l.name
and it.table_schema = 'public';

如果所有表都被找到,返回true,否则返回false

关于sql - 如何检查我所有表的存在?在 postgresql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37565826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com