In MySQL I want to sort by a field if that field exists as a column in the table. If that field does not exist I want the query to ignore that segment of the query. If I try something like
SELECT
post.*
FROM post AS post
WHERE post.postid IN (9818,9814,9815)
ORDER BY IFNULL(post.vote_count,0) > 5 DESC;
at the end of my query I get the error:
#1054 - Unknown column 'post.vote_count' in 'order clause'
The table structure for post has postid, threadid, and title fields. How should I write this query?
(I've seen Ordering by a field, only if it exists. That answer doesn't work for me since I can't add the earlier portion of the query, "IFNULL(post.vote_count,0) as newvotecount".)