I’m not quite sure how to do this:
SELECT contacts.first_name,
contacts.last_name,
(
SELECT quotes.id
FROM quotes
LEFT JOIN quotes_contacts ON quotes.id = quotes_contacts.quote_id
WHERE quotes.id IS NOT NULL
AND quotes_contacts.contact_id = contacts.id
ORDER BY quotes.date_entered
LIMIT 1
) AS 'first_quote'
FROM
contacts
But some of the values returned as first_quote are being returned as null. How can I stop that?
Rather than a subselect, use an
INNER JOINagainst the subquery. That will eliminate the NULLs.This can be simpler if you are certain that the
quote.idis incrementing, and the lowestquote.idpercontact_idalso has the earliestdate_entered. In that case you can just do aMIN(quote.id)