mysql - 在 mysql 查询中组合结果
问题描述
我有一个 mysql-table (我们称之为table),我在其中存储在游戏中得分的分数。
| ID | SCORE |
|----|-------|
|1 |10 |
|2 |8 |
|3 |15 |
|4 |9 |
我想知道是否有一种简洁的方法可以让一行包含最后一个分数(具有
最高 ID 的那个)和最高分?
目前我正在做
SELECT
score AS last_score,
(SELECT MAX(score) FROM table) AS best_score
FROM table
ORDER BY id DESC
LIMIT 1
这正是我想要的:
|last_score|best_score|
|----------|----------|
|9 |15 |
但是在性能方面,我想知道最好不要有两个select
:s
我宁愿做一些事情
SELECT
score AS last_score,
MAX(score) AS best_score
FROM table
ORDER BY id DESC
LIMIT 1
这给了我最好的分数,但不是最新的。
有什么建议么?
解决方案
select (SELECT score from purchase ORDER BY id desc LIMIT 1) as last_score, (SELECT MAX(score) as max_score from purchase) as max_score
推荐阅读
- powershell - 获取windows服务失败动作值
- authentication - Twilio 两因素身份验证是否支持对特定扩展的调用
- xamarin - Xamarin 形成警告
- javascript - NodeJS:基于 MongoDB 条目在特定时间发送大量文本的最佳方式?
- laravel - 防止在两次输入时提交多个表单
- html - Angular ng serve 不在本地主机上托管站点
- javascript - 如何使python lambda返回逗号运算符最后一个
- javascript - 是否可以在 NodeJs 中要求子 sdk 库?
- php - 如何在句子前剪切内容?
- javascript - Reactjs 函数未正确返回多个值:TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))