How to get smallest id in mysql using MIN () function

Table example

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 be as follows:

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

I want the result to be as follows:

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

Thanks in advance!

+6
source share
6 answers

The easiest way to get the smallest number id:

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

If you want to use MIN (as indicated in the header), one way to do this is:

SELECT id, name, price 
FROM sampleTable 
WHERE price = (SELECT MIN(price) from sampleTable)
+7
source

Use subquery:

SELECT id, name, price 
FROM table 
WHERE price = (SELECT min(price) FROM table);

Without LIMIT 1, this can return multiple rows that have a common minimum price. This may or may not be what you want, instead of arbitrarily choosing between the respective entries.

+4
source

MIN, ORDER BY LIMIT

SELECT id, name, price
FROM Table
ORDER BY Price ASC
LIMIT 1
+3

:

SELECT t1.id, t1.name, t1.price
FROM sampleTable AS t1
LEFT OUTER JOIN sampleTable AS t2
  ON t1.price > t2.price
WHERE t2.id IS NULL

, t2, , t1. t2 , t2. * NULL, , t1 .

, MySQL.

+3

;

SELECT id, name, price FROM table order by price ASC LIMIT 1

,

+1

( ): -

SELECT id, name, MIN(price) FROM (select * from table order by price) as t group by id

: join, subquery , .

.

0

All Articles