A
A
Anton2021-07-23 15:18:49
MySQL
Anton, 2021-07-23 15:18:49

Why are max() results not correct (mysql)?

There is a table with fields

meta_id, post_id, meta_key, meta_value Meta_value

values ​​are:
5247
5205
4028
3176
2123
2119
2119
2006
12800
12424
and so on... up to 70000
you need to display the largest of the range between 1000 and 30000.

I create a query

SELECT 
    max(meta_value)
FROM
    postmeta
WHERE
    meta_key = 'property_id'
        AND meta_value BETWEEN 1000 AND 30000;


When executing the query, it gives an incorrect value...
Displays 5247 instead of 13366 *(I scrolled through my eyes and see that this is the last value)

* If I specify the range like this BETWEEN 10000 AND 30000; then outputs the correct one (13366)

Why is that? Explain if not difficult, thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Semenov, 2021-11-17
Semenov

As a result, in order to sort such a table, I googled and applied such a hack
(meta_value * 1) <-- this converts the string 12345 into a number, and sorting works correctly.

SELECT distinct
    meta_value
FROM
    postmeta
WHERE
    meta_key = 'property_id'
        AND meta_value BETWEEN 1000 AND 30000
        ORDER BY (meta_value * 1) DESC;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question