6

I want to order the result by id in descending order and then LIMIT the number of rows obtained based on a @condition

ORDER BY id DESC
IF @condition is TRUE THEN  LIMIT 1
ELSE nothing
END IF
Himank Jog
  • 69
  • 1
  • 6

1 Answers1

10

You could use CASE:

ORDER BY id DESC
LIMIT CASE WHEN @condition THEN 1 END;

DBFiddle Demo

LIMIT NULL is the same as omitting the LIMIT clause

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275