开发者

mySQL - return best results

I want to have a q开发者_如何学JAVAuery that returns the best results from a table.

I am defining the best results to be the addition of two columns a + b (each column holds an int)

ie:

entry   a    b
1       4    5
2       3    2
3      20    30

Entry 3 would be returned because a + b is the highest in this case.

Is there a way to do this? One idea I had was to create another column in the table which holds the addition of a and b and then ORDER by DESC, but that seems a little bit messy.

Any ideas? Thanks!


SELECT  *
FROM    mytable
ORDER BY
        a + b DESC
LIMIT 1

Adding another column, however, would be a good option, since you could index this column which would improve the query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜