SQL query: how to translate IN() into a JOIN?(SQL 查询:如何将 IN() 转换为 JOIN?)
问题描述
我有很多这样的 SQL 查询:
I have a lot of SQL queries like this:
SELECT o.Id, o.attrib1, o.attrib2
FROM table1 o
WHERE o.Id IN (
SELECT DISTINCT Id
FROM table1
, table2
, table3
WHERE ...
)
这些查询必须在不同的数据库引擎(MySql、Oracle、DB2、MS-Sql、Hypersonic)上运行,所以我只能使用常见的 SQL 语法.
These queries have to run on different database engines (MySql, Oracle, DB2, MS-Sql, Hypersonic), so I can only use common SQL syntax.
这里 我读到了,用 MySqlIN
语句没有优化,而且速度很慢,所以我想将其切换为 JOIN
.
Here I read, that with MySql the IN
statement isn't optimized and it's really slow, so I want to switch this into a JOIN
.
我试过了:
SELECT o.Id, o.attrib1, o.attrib2
FROM table1 o, table2, table3
WHERE ...
但这并没有考虑到 DISTINCT
关键字.
But this does not take into account the DISTINCT
keyword.
问题:如何使用 JOIN
方法去除重复的行?
Question: How do I get rid of the duplicate rows using the JOIN
approach?
推荐答案
要使用 JOIN 编写此代码,您可以使用内部选择并与它连接:
To write this with a JOIN you can use an inner select and join with that:
SELECT o.Id, o.attrib1, o.attrib2 FROM table1 o
JOIN (
SELECT DISTINCT Id FROM table1, table2, table3 WHERE ...
) T1
ON o.id = T1.Id
我不确定这会更快,但也许……你可以自己尝试一下.
I'm not sure this will be much faster, but maybe... you can try it for yourself.
一般而言,将自己限制在可在多个数据库上运行的 SQL 不会带来最佳性能.
In general restricting yourself only to SQL that will work on multiple databases is not going to result in the best performance.
这篇关于SQL 查询:如何将 IN() 转换为 JOIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL 查询:如何将 IN() 转换为 JOIN?


- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- SQL 临时表问题 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 更改自动增量起始编号? 2021-01-01