gpt4 book ai didi

mysql - SQL查询显示同一个表中的多条记录?

转载 作者:行者123 更新时间:2023-11-29 06:12:33 25 4
gpt4 key购买 nike

我正在为一家小型初创大学体育招聘公司创建一个数据库。基本上,我想做的是提取大学两年高中球员的统计数据,如果可能的话,将它们显示在一行中。 (我知道这听起来很困惑,但为了我们正在使用的商业网络出版商,这种方式对我来说创建它更容易。)示例表结构如下所示:

Player ID, YearId, Points
10, 2010, 120
11, 2010, 90
10, 2011, 160
11, 2011, 173

期望的结果:

10, 2010, 120, 2011, 160

11, 2010, 90, 2011, 173

我是 MySQL 新手,因此我们将不胜感激。

最佳答案

这将为您提供滚动的前两年,从运行查询时自动使用当前年份和上一年:

SELECT p.PlayerId, p.YearId AS PreviousYear, p.Points AS PreviousPoints
, p2.YearId AS CurrentYear, p2.Points AS CurrentPoints
FROM Player AS p
LEFT JOIN Player AS p2 ON p.PlayerId = p2.PlayerId
AND p.YearId = p2.YearId - 1
WHERE p2.YearId = YEAR(NOW())

关于mysql - SQL查询显示同一个表中的多条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8302887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com