Basically what I'm trying to do is to get a list of purchases from one table and check if there is any attachments associated with the purchase and display 1
or 0
if there is/isn't an attachment found.
SELECT
CONCAT('#', LPAD(a.id, 5, '0')) as 'code',
a.id, a.value_total, DATE_FORMAT(a.date, '%d/%m/%Y') as 'date',
b.name as 'store',
CASE WHEN a.notes IS NOT NULL
THEN 1
ELSE 0
END AS notes
FROM
tb_purchase a,
tb_store b
WHERE a.id_store = b.id
AND a.id = :id_purchase // This line can be removed to get all the purchases or leave here to get just one with that specific ID
then I have a table called ts_purchase_att
where I store the attachments relation between purchases and attachments. The table has this structure:
id_purchase | id_attachment
32 | 47
32 | 127
33 | 68
38 | 97
I don't need to get the attachments, just check if there is at least one attachment related to that purchase.
I tried using something like this but it doesn't work
LEFT JOIN
( SELECT 1 FROM ts_purchase_att c WHERE c.id_purchase = a.id ) as attachment
What am I doing wrong?