gpt4 book ai didi

mysql - 比较mysql中的多个表

转载 作者:行者123 更新时间:2023-11-29 07:05:06 25 4
gpt4 key购买 nike

我有 4 个结构相同的表。这 4 个表是从另一个表(主表)生成的。

  • 主表 -> A(10000 行)
    • 表 1 -> a1(2000 行)
    • 表 2 -> a2(3000 行)
    • 表 3 -> a3(4000 行)
    • 表 4 -> a4(1000 行)

假设生成的4张表是不相交的,它们的和等于表A。

但我需要确认这一点。

我的问题:我如何比较这 4 个表,看看它们是否有任何共同的 ID?

我知道我可以构建很多查询来内部连接 ​​a1 和 a2、a1 和 a3 等等。

但我认为这应该是另一种更好的方式。

最佳答案

我只是union all每个表中的所有id,group by每个,然后检查是否有任何计数超过1

select count(*) from (
select id from table1 union all
select id from table2 union all
select id from table3 union all
select id from table4) group by id having count(*)>1

如果任何 id 出现不止一次,它将显示在此查询的结果中。

关于mysql - 比较mysql中的多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7807560/

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