【问题标题】:Selecting data from two different tables without using joins从两个不同的表中选择数据而不使用连接
【发布时间】:2013-06-30 03:00:58
【问题描述】:

我有table1 和table2,需要从他们每个人那里获取数据。

表 1

"id"    "name"      "description"
"1"     "Windows"   "Microsoft Windows 8"

表2

"id" "type" "name"              "description"
"1"  "22"   "Microsoft Windows" "Microsoft Windows 8 Home"
"2"  "2"    "Not an Edit"       "Not an Edit"

我是这样选择的

select table1.name, table1.description, 
table2.name, table2.description 
from table1,table2 
where table2.id=table1.id and table2.`type`=22;

当一次选择大约 500 多行时,使用内连接会更快或更有效吗?

我见过大多数使用内连接来执行此操作的示例。

【问题讨论】:

  • 我不知道您在哪里可以找到这样的内部连接示例。只需使用明确的INNER JOIN
  • 您在示例查询中使用了 JOIN,只是使用了不推荐使用的语法。
  • 比什么更快或更高效?那是一个 INNER JOIN;它只是写成“有趣”。

标签: mysql sql


【解决方案1】:

没有区别,只是语法不同,内部它们产生相同的执行计划:

ANSI vs. non-ANSI SQL JOIN syntax

【讨论】:

    【解决方案2】:

    你可以这样做..

    select table1.name, table1.description, 
    table2.name, table2.description 
    from table1 inner join Table2 on  table2.id=table1.id and table2.`type`=22
    

    【讨论】:

      【解决方案3】:

      这是没有加入的正确答案

      select t1.*,t2.* from t1,t2 where t1.id=t2.id;
      

      【讨论】:

      • 逗号是交叉连接。 (优先级低于关键字连接。)这也不能回答问题。此外,此查询不会返回问题中的查询所做的事情。
      猜你喜欢
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 2012-12-28
      • 2017-02-20
      相关资源
      最近更新 更多