sql检查或多表中一列的唯一性

uqzxnwby  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(385)

我在多个表中有一个“unique”列“gid\u new”。有没有一种方法可以检查它在sql中的qgis项目中的所有表中是否是唯一的?
它可以在一个sql搜索中完成,而不必将表合并成一个表,然后运行类似

SELECT A.GID_New, count(*), A.TableName
FROM "Water_Merged" as A
Group by A.GID_New

然后检查计数是否大于1
我想知道哪一个表的非唯一的gidèu的新的是来自以及。
数据在qgis的geopackage中,因此代码需要在qgis sql实现中工作。

j2datikz

j2datikz1#

你可以用 union all :

select gid_new, count(*) no_matches
from (
    select gid_new from table1
    union all select gid_new from table2
    union all select gid_new from table3
) t
group by gid
having count(*) > 1

如果您想知道哪些表中存在重复项,那么一个选项是字符串串联。假设您的数据库使用 string_agg() ,看起来像:

select gid_new, count(*) no_matches, string_agg(which, ',') which_tables
from (
    select 'table1' which, gid_new from table1
    union all select 'table2', gid_new from table2
    union all select 'table3', gid_new from table3
) t
group by gid
having count(*) > 1

相关问题