【问题标题】:Query in 2 tables - JOIN [duplicate]在 2 个表中查询 - JOIN [重复]
【发布时间】:2018-04-03 07:55:16
【问题描述】:

我在 MySQL 中苦苦挣扎。我有 2 个表,一个包含所有数据的表和一个包含有关节点的一般信息的表。

数据表

  • 身份证
  • 姓名

节点表

  • 身份证
  • 地址

现在我有了地址,我想构建一个返回 Name 的查询。我可能应该使用 JOIN,但我上次使用 MySQL 可以追溯到很久以前。

提前致谢

【问题讨论】:

  • 请分享一些示例数据以及您尝试获得的结果

标签: mysql join


【解决方案1】:
select
    dataTable.name
from
    dataTable
    inner join nodeTable on
        dataTable.ID = nodeTable.ID   
where
    nodeTable.adress = 'your address'

【讨论】:

  • 非常感谢。我需要经常玩 mysql :-)
【解决方案2】:

以下代码是否满足您的需求?

select Name 
  from dataTable, nodeTable 
 where dataTable.ID = nodeTable.ID
   and Address = 'Rathausstrasse'

【讨论】:

  • 不要鼓励 25 岁以上的旧 JOIN 样式使用正确的 INNER JOIN 语法
  • 它只是样式 - 它不会影响性能 - 现在查询优化器已经足够聪明了......
  • 新的 INNER JOIN 语法更容易阅读和理解,性能方面确实没有区别
【解决方案3】:
select d.name
from dataTable d, nodeTable n
on d.id = n.id
where n.address = '10 Main St.';

【讨论】:

  • 不要鼓励 25 岁以上的旧 JOIN 样式使用正确的 INNER JOIN 语法
猜你喜欢
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
相关资源
最近更新 更多