【问题标题】:Select everything from two table based on the same ID根据相同的 ID 从两个表中选择所有内容
【发布时间】:2014-04-10 14:10:25
【问题描述】:

我有两张桌子:

base_profile: id、first_name、last_name、地址

flight_profile: id、航班号、目的地

如何根据相同的 id 从这两个表中选择所有字段? 我的假设是:

SELECT * 
FROM base_profile, flight_profile WHEN base_profile.id == flight_profile.id 
WHERE id, first_name,last_name,address,flight_no,destination

我知道这是不对的。任何人都可以帮我纠正它吗?谢谢。

【问题讨论】:

  • SELECT * FROM base_profile, flight_profile WHERE base_profile.id == flight_profile.id;

标签: mysql sql database join relational-database


【解决方案1】:

使用inner join

SELECT base_profile.id, base_profile.first_name, base_profile.last_name, base_profile.address,
      flight_profile.flight_no,flight_profile.destination
FROM base_profile INNER JOIN  flight_profile
     ON base_profile.id = flight_profile.id

或更一般地

SELECT <fields you want to return>
FROM <tables linked with joins>

【讨论】:

    【解决方案2】:

    如何从上面的代码(示例)中选择 ID = 1 的内连接

    SELECT base_profile.id, 
           base_profile.first_name, 
           base_profile.last_name, 
           base_profile.address, 
           flight_profile.flight_no, 
           flight_profile.destination 
    FROM   base_profile 
           INNER JOIN flight_profile 
                   ON base_profile.id = flight_profile.id 
    WHERE  base_profile.id = 1  
    

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多