SELECT patients.*, IF(grouped_priem.card_id IS NULL, 0, 1) AS has_priem FROM patients LEFT JOIN (SELECT card_id FROM priem GROUP BY card_id) AS grouped_priem ON patients.card_id = grouped_priem.card_id
if ($row['has_priem'] == '1') { echo "<Цвет шрифта красный>"; }