根据同一个 mysql 表中的 id 添加点列

发布于 2024-11-27 19:13:26 字数 1684 浏览 1 评论 0原文

好的,数据库的布局如下(仅列出正在使用的列):

Table Name: race_stats
Columns: race_id, user_id, points, tournament_id

Table Name: user
Columns: user_id, driver

Table Name: race
Columns: race_id, race_name

Table Name: tournament
Columns: tournament_id, tournament_name

这是我当前的查询:

$query = "

SELECT user.user_id, user.driver, race_stats.points, race_stats.user_id, 
   SUM(race_stats.points) AS total_points "." 
FROM user, race_stats, tournament, race "." 
WHERE race.race_id=race_stats.race_id 
   AND user.user_id=race_stats.user_id 
   AND tournament.tournament_id=race_stats.tournament_id 
GROUP BY driver 
ORDER BY total_points DESC 
LIMIT 0, 15

";

好的,查询有效,但它将 race_stats.race_id 列中所有可用比赛的所有比赛加起来作为总分。我已经绞尽脑汁来解决这个问题,但我似乎找不到我需要的解决方案。我确信这一定是一个简单的修复,但我就是无法得到它。非常感谢任何帮助。

///////////////////用原始值编辑

INSERT INTO `race_stats` (`id_race`, `race_id`, `user_id`, `f`, `s`, `race_interval`, `race_laps`, `led`, `points`, `total_points`, `race_status`, `tournament_id`, `driver`, `tournament_name`) VALUES

(1, 1, 4, 1, 4, '135.878', 60, '2', 180, 0, 'Running', 1, 'new_driver_5', ''),
(2, 1, 2, 2, 2, '-0.08', 60, '22', 175, 0, 'Running', 1, 'new_driver_38', ''),
(3, 1, 5, 3, 5, '-11.82', 60, '2', 170, 0, 'Running', 1, 'new_driver_94', ''),
(4, 2, 2, 1, 15, '138.691', 29, '6', 180, 0, 'Running', 2, 'new_driver_38', ''),
(5, 2, 15, 2, 9, '-16.12', 29, '8*', 180, 0, 'Running', 2, 'new_driver_44', ''),
(6, 2, 8, 3, 11, '-2:03.48', 29, '0', 165, 0, 'Running', 2, 'new_driver_83', ''),

//////////////////////////////////////////////////////////////////////////就是你所说的原始值的意思,如果不是的话我可以为你获取更多数据。

OK the database is layed out as (only columns being used are listed):

Table Name: race_stats
Columns: race_id, user_id, points, tournament_id

Table Name: user
Columns: user_id, driver

Table Name: race
Columns: race_id, race_name

Table Name: tournament
Columns: tournament_id, tournament_name

This is my current query:

$query = "

SELECT user.user_id, user.driver, race_stats.points, race_stats.user_id, 
   SUM(race_stats.points) AS total_points "." 
FROM user, race_stats, tournament, race "." 
WHERE race.race_id=race_stats.race_id 
   AND user.user_id=race_stats.user_id 
   AND tournament.tournament_id=race_stats.tournament_id 
GROUP BY driver 
ORDER BY total_points DESC 
LIMIT 0, 15

";

Ok the query works but it is adding them all up for all the available races from the race_stats.race_id column as the total points. I have racked my brain beyond recognition to fix this but I just can't quite seem to find the solution I need. I'm sure it has to be an easy fix but I just can't get it. Any help is greatly appreciated.

///////////////////EDITED WITH RAW VALUES//////////////////////

INSERT INTO `race_stats` (`id_race`, `race_id`, `user_id`, `f`, `s`, `race_interval`, `race_laps`, `led`, `points`, `total_points`, `race_status`, `tournament_id`, `driver`, `tournament_name`) VALUES

(1, 1, 4, 1, 4, '135.878', 60, '2', 180, 0, 'Running', 1, 'new_driver_5', ''),
(2, 1, 2, 2, 2, '-0.08', 60, '22', 175, 0, 'Running', 1, 'new_driver_38', ''),
(3, 1, 5, 3, 5, '-11.82', 60, '2', 170, 0, 'Running', 1, 'new_driver_94', ''),
(4, 2, 2, 1, 15, '138.691', 29, '6', 180, 0, 'Running', 2, 'new_driver_38', ''),
(5, 2, 15, 2, 9, '-16.12', 29, '8*', 180, 0, 'Running', 2, 'new_driver_44', ''),
(6, 2, 8, 3, 11, '-2:03.48', 29, '0', 165, 0, 'Running', 2, 'new_driver_83', ''),

Let me know if this is what you meant by raw values if not I can get some more data for you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-12-04 19:13:26

为了完整性,仅在此处发布解决方案:

SELECT user.driver, race_stats.race_id, 
   SUM(race_stats.points) AS total_points "." 
FROM user, race_stats "." 
WHERE user.user_id=race_stats.user_id 
GROUP BY user.driver, race.race_id

Just posting the solution here for completeness:

SELECT user.driver, race_stats.race_id, 
   SUM(race_stats.points) AS total_points "." 
FROM user, race_stats "." 
WHERE user.user_id=race_stats.user_id 
GROUP BY user.driver, race.race_id
阳光下的泡沫是彩色的 2024-12-04 19:13:26

这是您想要的查询(为了便于阅读而格式化):

SELECT
    u.driver,
    SUM(rs.points) AS total_points 
FROM user u
LEFT JOIN race_stats rs on rs.user_id = u.user_id
GROUP BY 1;

使用外连接(即 LEFT JOIN )的优点是没有统计数据的驱动程序仍然可以获得一行,但带有 null 。代码> 作为total_points。

ps 我不知道您的查询中 "." 的用法是什么,所以我删除了它。

Here's the query you want (formatted for readability):

SELECT
    u.driver,
    SUM(rs.points) AS total_points 
FROM user u
LEFT JOIN race_stats rs on rs.user_id = u.user_id
GROUP BY 1;

The advantage of using an outer join (ie LEFT JOIN) is that drivers who have no stats still get a row, but with null as total_points.

p.s. I don't know what the usage of "." in your query is all about, so I removed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文