【问题标题】:multiple self join replacement多个自连接替换
【发布时间】:2012-09-03 18:15:45
【问题描述】:

想要替换表上的多个自连接。 SQL中表自连接的最大限制是多少

我有一个表 table_1,列如下:

uid number(10),
a_name nvarchar(20),
aaId number (10),

唯一约束uid和aaId的组合中存在。

表中的数据是这样的。 每个 aaId 大约有 90 个 Uid。

现在我的查询是这样的

select aaId from table_1 
  inner join table_1 t1 on t1.uid=9 and t1.a_name like 'a'  
  inner join table_1 t2 on t2.uid=8 and t2.a_name like 'ab'  
  inner join table_1 t3 on t3.uid=7 and t3.a_name like 'ac'

我的问题是 内部连接的数量已增加到 90 。 由于表中的行数约为 20 万,因此该查询是否有效。 或者,如果我可以做任何其他事情来替换大量的自连接。

请帮忙。

提前致谢。

【问题讨论】:

  • 为什么要使用内连接?它的用途是什么,您要提取什么数据?
  • 是的,请解释您要做什么。从您的代码中一点也不明显。我猜您是在尝试仅选择表中具有完整记录集的 aaId,但这很难说。
  • 欢迎提供示例(数据样本及其预期结果)。

标签: sql join self-join


【解决方案1】:

首先,为什么不使用 OR 语句的 WHERE 部分?

select aaId from table_1 
WHERE (table_1.uid=9 and table_1.a_name like 'a') OR 
(table_1.uid=8 and table_1.a_name like 'ab') OR
(table_1.uid=7 and table_1.a_name like 'ac')

【讨论】:

    【解决方案2】:

    请尝试使用自连接和所需条件的查询

    从 table_1 t1、table_1 t2 中选择 aaId 其中 (t1.uid=9 and t2.a_name like 'a') 或 (t1.uid=8 and t2.a_name like 'ab')

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多