获得最大分数并存储在另一列中
这是我的表:
student_id | objectig_id | 总数 | 最大 |
---|---|---|---|
101 | 1 | 90 | |
102 | 2 | 80 | |
103 | 1 | 95 | |
101 | 1 | 82 | |
103 | 2 | 76 | |
104 | 1 | 95 | |
101 | 2 | 71 |
我想在特定主题中获得最大总数,并随时将其存储在另一列中列插入或更新。
桌子的外观:
student_id | object_id | 总数 | 最大 |
---|---|---|---|
101 | 1 | 90 | 95 |
102 | 2 | 80 | 71 |
103 | 1 | 95 | 95 |
101 | 1 | 82 | 95 |
103 | 2 | 76 | 80 |
104 | 1 | 95 | 95 95 |
101 | 2 | 80 | 这就是我希望 |
我尝试了此询问与所有学生_ID相比,不会在每个主题中添加最大分数。
SELECT MAX(`total`) AS highest
FROM results
GROUP BY student_id
我想我应该为此使用触发器,但是我的正常查询也可以。
This is my table:
student_id | subject_id | total | max |
---|---|---|---|
101 | 1 | 90 | |
102 | 2 | 80 | |
103 | 1 | 95 | |
101 | 1 | 82 | |
103 | 2 | 76 | |
104 | 1 | 95 | |
101 | 2 | 71 |
I want to get the max total in a particular subject and store it in another column whenever the total column is inserted or updated.
This is what I want the table to look like:
student_id | subject_id | total | max |
---|---|---|---|
101 | 1 | 90 | 95 |
102 | 2 | 80 | 80 |
103 | 1 | 95 | 95 |
101 | 1 | 82 | 95 |
103 | 2 | 76 | 80 |
104 | 1 | 95 | 95 |
101 | 2 | 71 | 80 |
I have tried this query but it doesn't add the max scores in each subject against all the student_id.
SELECT MAX(`total`) AS highest
FROM results
GROUP BY student_id
I suppose I should use a trigger for this but the normal query is also okay by me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的表格中尚不存在,则需要使用
Alter
语句添加它。然后,您可以首先使用聚合选择每个“ max ”值“ objection_id ”,然后在
update> update> update>更新
语句中使用它来利用a <<代码>加入操作:检查演示在这里。
If your column does not yet exist in your table, you need to add it with an
ALTER
statement.Then you can first select the "max" value for each "subject_id" using an aggregation, then use it inside the
UPDATE
statement exploiting aJOIN
operation:Check the demo here.
假设您正在使用MySQL 8+。
理想情况下,您应该在选择这样的同时获得此类数据,而不是将Maxtotal数据存储在每列中:
但是,如果您仍然需要更新它,请使用
Update
与join> join >
更新后的输出:
参见this
Assuming you are using MySQL 8+.
Ideally, instead of storing MaxTotal data into each column, you should get such the data while selecting like this:
However, if you still need to update it anyway, use
UPDATE
withJOIN
Output after the update:
See this db<>fiddle.