Skip to content
Advertisement

SQL order by a function with literal values

I want to order some SQL result using the result of a calculation. I can select this

SELECT sqrt( POWER(TERM_0 - -0.12985125184059143,2)) from faces f limit 3

So the syntax seems correct, but when I try to use it in the order by clause like this:

SELECT * from faces f limit 3
order by
sqrt( POWER(TERM_0 - -0.12985125184059143,2))

I get a syntax error. So what is the correct way of doing this? I’m using mariadb as backend, not sure if that makes any difference from normal mysql.

Advertisement

Answer

Just put the order by before limit order :

SELECT *
from faces f 
order by sqrt( POWER(TERM_0 - -0.12985125184059143,2)) 
limit 3

Here is a demo:

DEMO

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement