Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the id of smallest number in mysql using the MIN() function

Tags:

sql

mysql

sample table

id |   name   | price |
-----------------------
1  |   john   | 300   |
----------------------- 
2  | michael  |  400  |
----------------------- 
3  | michelle |  250  |
-----------------------

I will get the smallest number in the table using this query

SELECT id, name, MIN(price) FROM table

The result will become this:

_______________________
id |   name   | price |
-----------------------
1  | michelle |  250  |

I want the result will become like this:

id |   name   | price |
-----------------------
3  | michelle |  250  |
-----------------------

Thanks in advance!

like image 415
Nejimz Avatar asked Nov 24 '11 01:11

Nejimz


1 Answers

The easiest way to get the id of the smallest number is this:

SELECT Id, name, price
FROM sampleTable
ORDER BY price ASC 
LIMIT 1;

If you want to use MIN (as the title states), one way to do it would be this:

SELECT id, name, price 
FROM sampleTable 
WHERE price = (SELECT MIN(price) from sampleTable)
like image 113
Code Magician Avatar answered Oct 20 '22 15:10

Code Magician