WITH RECURSIVE cte AS (
SELECT
a.*
FROM
tblname a
WHERE
ifnull( a.user_account, '' ) != ifnull( a.user_referrer_code, '' )
AND a.user_account = 'test' UNION ALL
SELECT
K.*
FROM
tblname k
INNER JOIN cte c ON c.user_account = k.user_referrer_code
AND k.user_account != k.user_referrer_code
) SELECT
*
FROM
cte
WHERE
EXISTS ( SELECT 1 FROM tblname2 WHERE cust_no = cte.user_account AND is_available = 1 )