Bring certain record first in SQL query

Asked

Viewed 62 times

1

Hello! All right?

I have an image registration system on a portfolio page.

In my database I have a table with a column for the image path and a column called main which is a char(1) which can be’S' or 'N'.

On this system I need the record that has’S' in the main field to be the first line in the query result.

Because the main image needs to be the first record in the result of the SQL query in order to mount the gallery.

There is a way in Mysql to do this.

  • There is yes, put your query to help with that

1 answer

0


Make the adjustment on ORDER BY of query:

...
ORDER BY CASE principal WHEN 'S' THEN 0 ELSE 1 END;

The clause ORDER BY indicates how the data presentation will be sorted. In the above case we say that if the column principal contain the value S then the first order check will receive 0.

  • 1

    Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.