I have two tables. One with information about properties. The other stores 3 images for each property. Of these three images - one is marked as being the "main" image.
So I have:
Properties:
p_id name
1 villa a
2 villa b
3 villa c
4 villa d
and
Images
i_id p_id main
1 1 0
2 1 0
3 1 1
4 2 0
5 2 1
6 2 0
I need to produce a query which returns all of the properties with the id of their main image. e.g.
p_id name i_id
1 villa a 3
2 villa b 5
I know this will involve using LIMIT 1 and a join, but not sure where to start, I have already attempted doing this by using a subquery but felt it must be less complicated than what I was doing....
* HOW DO I * Make it so it orders the query by "main" selecting the top 1 (i.e. so if main is not set it will still select an image) ?