I have 2 tables. Persons and Images.
One person may have many images.
I want to select a person with his primary image BUT(!), if none of the images marked as isPrimary=1 bring the first one.
Only one or less images may be isPrimary=1
SELECT
*,
(
SELECT id
FROM Image
WHERES personId=r.id AND isPrimary=1
LIMIT 1
) AS primaryImageId
FROM Persons r
ORDER BY id DESC;
I did it with subselect, join is also good... Thanks