在SQL中,我们可以使用“Union”来表示。合并两个表。 “交叉”有哪些不同的方法?
在 SQL 中,有一个运算符可以“联合”两个表。在一次采访中,有人告诉我,假设一个表只有 1 个字段,其中有 1、2、7、8,而另一个表也只有 1 个字段,其中有 2、7,我如何获得交集。一开始我很震惊,因为我从来没有这样看过。
后来我发现它实际上是一个“Join”(内连接),这只是
select * from t1, t2 where t1.number = t2.number
(虽然“join”这个名字感觉更像是“union”而不是“intersect”)
另一种解决方案似乎是
select * from t1 INTERSECT select * from t2
但不支持在 MySQL 中。除了这两种方法之外,还有其他方法可以获取交集吗?
In SQL, there is an operator to "Union" two tables. In an interview, I was told that, say one table has just 1 field with 1, 2, 7, 8 in it, and another table also has just 1 field with 2, and 7 in it, how do I get the intersection. I was stunned at first, because I never saw it that way.
Later on, I found that it is actually a "Join" (inner join), which is just
select * from t1, t2 where t1.number = t2.number
(although the name "join" feels more like "union" rather than "intersect")
another solution seems to be
select * from t1 INTERSECT select * from t2
but it is not supported in MySQL. Are there different ways to get the intersection besides these two methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个页面解释了如何在MySQL中实现INTERSECT和MINUS。要实现 INTERSECT,您应该使用内部联接:
您的代码也执行此操作,但不建议再编写这样的联接。
This page explains how to implement INTERSECT and MINUS in MySQL. To implement INTERSECT you should use an inner join:
Your code does this too, but it is not recommended to write joins like that any more.
相交只是一个内连接。所以
可以像MySQL一样重写
An intersect is just an inner join. So
can be rewritten for MySQL like