Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL How to select Max 5 values from table?

Tags:

sql

mysql

max

In my database I have two tables and want to select highest 5 values from table but I only get first highest value cannot get more than one value.

Here is my SQL,

SELECT * FROM table1 WHERE id=(SELECT id, MAX(num1+num2) FROM table2 limit 5)

How can I get first top 5 highest values?

Thanks.

like image 343
gzml Avatar asked Oct 14 '14 06:10

gzml


1 Answers

This should do it

SELECT id, num1 + num2 AS total FROM table1 ORDER BY num1 + num2 DESC LIMIT 5
like image 153
fallenland Avatar answered Oct 02 '22 01:10

fallenland