Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

EbrahemSamer's avatar

How to arrange mysql query result according to where conditions ?

I am running query like

$query = "SELECT * FROM students WHERE name = ahmed or age > 20";

the result will be any person has name ahmed or greater than 20 years

I want the name result get first ( all students have this name 'ahmed' ) not the age .

How can i do this ?

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

Order by name='ahmed' descending:

$query = "SELECT * FROM students WHERE name = ahmed or age > 20 ORDER BY name = 'ahmed' DESC";

You are ordering on the truthiness of the expression; the name='ahmed' will evaluate as either true or false (1 or 0).

Please or to participate in this conversation.