L
L
Lelouch Lamperouche2021-07-16 21:00:07
MySQL
Lelouch Lamperouche, 2021-07-16 21:00:07

Why does ORDER BY DESC in mysql not work 100% of the time?

There is a database with the INTEGER type, different values ​​are entered there by first reducing them to 2 characters after the comma: . There are both negative and positive values, 0 is also present, but there are no empty "Null" cells. The crux of the problem: when usingprice = float("%.2f" % pricemore)


cursor.execute("SELECT * FROM table WHERE status = '1'  ORDER BY price DESC ")
la = cursor.fetchall()

and further indecencies with the received data SOMETIMES (as a rule, exclusively at the first start), the data is not sorted, but simply taken in the order in which it was placed in the table.

However, when I stop the script and run the code again, it sorts normally.

What could be the reason for such sorting failures? They are deadly in my case.

And is there any analogue from the python side, so that it sorts by the base, and not the base itself, because As mentioned above, it crashes.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
DevMan, 2021-07-16
@Lelush

ORDER BY DESC always works unambiguously and in 100% of cases.
if it is not: the problem is on the receiving side.

O
o5a, 2021-07-16
@o5a

Lelouch Lamperouch ,

And further lewdness with the data obtained

So maybe the reasons are in these indecencies of yours, and not in the fact that sorting does not work. Do you perform these data manipulations that sort and knock down? By itself, ORDER BY will not fail, at least in the form that was given in the question.
And is there any analogue from the python side, so that it sorts by the base, and not the base itself, because As mentioned above, it crashes.

There is

R
rPman, 2021-07-16
@rPman

Recheck that the type of the field is really numeric and not, for example, varchar (although if there are numbers, then this should not change anything)
Recheck that the database is not damaged, can you just recreate the index?

S
SagePtr, 2021-07-17
@SagePtr

Maybe the browser takes from the cache the original version of the page that it saved before sorting was added there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question