hot_programmer
hot_programmer

Reputation: 51

Get Wrong Query when usin MIN in sql

I have an table like this

table

I have been try this sql code

SELECT id,lat,lng,name,MIN(hitung) AS Smallest FROM open_list;

but the result give me wrong query

wrong

what i wanna do is being like this :

reall

Upvotes: 0

Views: 33

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

You could do this:

SELECT id,lat,lng,name,hitung
FROM open_list
ORDER BY hitung ASC
LIMIT 1

Or, if you want to do more complex stuff, start with this:

SELECT id,lat,lng,name,hitung
FROM open_list
JOIN (
    SELECT MIN(hitung) as hitung
    FROM open_list
) tmp USING (hitung)

Upvotes: 2

Hart CO
Hart CO

Reputation: 34774

When you do not GROUP BY non-aggregate values from your SELECT list, the returned values are arbitrary. You can add a GROUP BY but that will return multiple records, you can use ORDER BY and LIMIT to get what you're after:

SELECT id,lat,lng,name,hitung
FROM open_list
GROUP BY id,lat,lng,name
ORDER BY hitung ASC
LIMIT 1;

Upvotes: 1

Related Questions