简体   繁体   English

在 mysql 查询中组合结果

[英]Combining results in a mysql-query

I have a mysql-table (lets call it table ) where I store points scored in a game.我有一个 mysql-table (我们称之为table ),我在其中存储在游戏中得分的分数。

| ID | SCORE |
|----|-------|
|1   |10     |
|2   |8      |
|3   |15     |
|4   |9      |



What I'm wondering if there is a neat way to get one row containing the last score (the one with the我想知道是否有一种巧妙的方法来获得包含最后一个分数的一行(带有
highest ID) and the highest score?最高ID)和最高分?


Currently I'm doing目前我正在做

SELECT
    score AS last_score,
    (SELECT MAX(score) FROM table) AS best_score
FROM table
ORDER BY id DESC
LIMIT 1

which gives me exactly what I want:这正是我想要的:

|last_score|best_score|
|----------|----------|
|9         |15        |

But performance wise I wonder if it's better not to have two select :s但是性能方面我想知道最好不要有两个select :s


I would prefer to do something in the lines of我宁愿做一些事情

SELECT
    score AS last_score,
    MAX(score) AS best_score
FROM table
ORDER BY id DESC
LIMIT 1

which gives me the best score, but not the latest.这给了我最好的分数,但不是最新的。


Any suggestions?有什么建议么?

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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM