Reputation: 376
I have two tables: posts and custom_fields.
A post can have multiple custom_fields.
Posts table: id | title
Custom Fields table: id | post_id | custom_key | custom_value
What I want is show all posts, but show first the featured ones which are the ones that have: "custom_key = 'featured' AND custom_value='1'".
Not every posts have the custom_key="featured".
How can I make a mysql query that lists in this way?
Thank you!
Upvotes: 1
Views: 451
Reputation: 17275
Something like that should work:
SELECT * FROM posts LEFT JOIN customfields
ON post_id=posts.id AND custom_key='featured'
ORDER BY custom_value=1 DESC, id DESC;
Upvotes: 1