PHP:强制'精选'到顶部

I have database tables with a column called "Featured", which is either y or n.

I'm trying to force any 'Featured'=='y' to print first (it prints HTML through a foreach loop).

Right now it just loads in the order of the index (ID).

I tried adding ORDER BY Featured to the end of my SQL query, but it does nothing.

How can I accomplish this?

I'm using this through a generic function. What can I do to check to see if "Featured" exists without making 2 queries?

It breaks the SQL on tables that do not contain 'Featured'

I set it up with a switch to change the SQL only as needed.. but I would much rather handle this in the SQL statement if anyone knows how to mix IF EXISTS in. Thanks for the help!

Instead of using Y and N values, use a more numerical approach like 1 and 0, this way you can use numerical sorting. But if you really don't want to change your column values, then just do

ORDER BY Featured DESC

as this will put all the Ys first.

PS: In the future, you'll find it's better to use numbers for boolean (1 and 0), this will give you the ability to add niche features in the future where you'll have a 3rd and 4th scenario (so you can set it to 2 and 3, etc).