开发者

mySQL - return best results

开发者 https://www.devze.com 2023-02-27 03:15 出处:网络
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)

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

精彩评论

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