比较mysql中的多个表

发布于 2024-12-10 16:32:31 字数 391 浏览 0 评论 0原文

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

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

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

但我需要确认这一点。

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

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

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

I have 4 tables with the same structure.
This 4 tables are generated from another one, the main table.

  • Main table -> A (10 000 rows)
    • table 1 -> a1 (2 000 rows)
    • table 2 -> a2 (3 000 rows)
    • table 3 -> a3 (4 000 rows)
    • table 4 -> a4 (1 000 rows)

It's supposed that 4 generated tables are disjoint, and their sum is equal to table A.

But i need to confirm this.

My question: How i compare this 4 tables, to see if they have any id in common?

I know that i can build a lot of querys to inner join a1 with a2, a1 with a3 and so on.

But i think that should be another better way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2024-12-17 16:32:31

我只是将每个表中的所有 idunion all 组合在一起,group by 每个,并检查是否有任何 id 计数超过 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 出现多次,它将显示在该查询的结果中。

I would just union all together all the ids from each table, group by each, and check whether any have counts more than one

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

If any id occurs more than once, it will show up in the results of this query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文