i have three table
cards (id,...)
order_cards (card_id, order_id....)
transactions (order_card_id, status, ....)
i want to get all cards that had last 7 transactions failed
i am trying something like that
select cards.*,
(
select sum(case tmp1.status when 'fail' then 1 else 0 end) from
(select transactions.status from order_cards
left join transactions on transactions.order_card_id = order_cards.id
where order_cards.card_id = cards.id
order by transactions.id desc limit 7
) as tmp1
) as total_fail
from cards
group by cards.id
having total_fail > 5
getting this error
Unknown column 'cards.id' in 'where clause'
issue with above query is parent id not working in level 2 subquery. tried to use having clause with count but its not working with limit any suggestion thanks
already tried
Use column of parent query in subquery of a subquery
MYSQL - Get all records that have more than 1 record for the same id