【问题标题】:SQL - Query between two tables (top and bottom)SQL - 两个表之间的查询(顶部和底部)
【发布时间】:2016-03-17 15:52:15
【问题描述】:

我在创建 2 个表之间的查询时遇到了困难。

表试点

Id (Primary Key)
Name

餐桌季节

Year (Primary key)
Pilot_id (Foreign Key)

我只想查询每个飞行员的第一年和最后一年

【问题讨论】:

  • 你想join Pilot ID 上的表,然后select max(year), min(year), idgroup by id
  • 如果您无法完成此操作,请向我们展示您的尝试,以便我们更好地了解您需要哪些帮助。

标签: sql sql-server database relational-database


【解决方案1】:

所以基本上是这样的:

SELECT t.id,t.name,max(s.year),min(s.year)
FROM Pilot t
INNER JOIN Season s
 ON(t.id = s.pilot_id)
GROUP BY t.id,t.name

【讨论】:

  • 非常感谢。这正是我想要做的! =) 干杯
【解决方案2】:

给你!

Select Id, Name,
    (Select Min(Year) from Season Where Pilot_Id = Id) as FirstYear,
    (Select Max(Year) from Season Where Pilot_Id = Id) as LastYear
  from Pilot  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2017-09-25
    • 1970-01-01
    相关资源
    最近更新 更多