mysql什么时候使用子查询,什么时候使用表连接查询,关系多张表的时候该怎么查询

2025-02-26 09:36:18
推荐回答(2个)
回答1:

1,表关联的效率要高于子查询,因为子查询走的是笛卡尔积
2,表关联可能有多条记录,子查询只有一条记录,如果需要唯一的列,最好走子查询

回答2:

Semi-join 限制

不过并不是所有子查询都是半联接,必须满足以下条件:

  • 子查询必须是出现在顶层的 WHERE、ON 子句后面的 IN 或者 =ANY
  • 子查询必须是单个 select,不能是 union;
  • 子查询不能有 group by 或者 having 子句(可以用 semijoin materialization 策略,其他不可以 );
  • It must not be implicitly grouped (it must contain no aggregate functions). (不知道啥意思,保持原文);
  • 子查询不能有 order by with limit;
  • 父查询中不能有 STRAIGHT_JOIN 指定联接顺序;
  • The number of outer and inner tables together must be less than the maximum number of tables permitted in a join.