I have two tables:
table: ads
fields: id, price
table: cars
fields: id, ad_id, year
I want to be able to order the results by cars.year – however not all results will have an entry in the cars table, i.e. some results may only have entries in ads.
At the moment results without entries are being returned first by my “order by cars.year ASC, ads.price ASC” clause, I want results that don’t have cars entries to appear after the results that do.
Any ideas how I can with this ORDER BY clause?
Thanks,
You can use an ORDER BY clause similar to the following:
This will put the rows where
cars.idis not null first, sorted by descending price. Then afterwards will be the rows wherecars.idis null, also sorted by descending price.It’s not completely clear to me from your question what ordering you want, so you may have to adjust this answer slightly to fit your needs.