'MySQL query with WHERE subquery does not work on no result

I have written a complex query in MySQL to return a result.
it works perfectly

Until .....

the subquery returns no result
how to write an if or IF null then substitute in date '2020-06-03'
any help greatly appreciated


SELECT
    * 
FROM
    trades 
WHERE
    stock_code = 'IHVV' 
    AND acc_id = '4' 
    AND tx_date > 
    (SELECT tx_date 
    FROM
        ( SELECT *, ( @sum_units := @sum_units + units ) AS sum_units 
        FROM
            trades
            JOIN ( SELECT @sum_units := 0 ) params 
        WHERE
            stock_code = 'IHVV' 
            AND acc_id = '4' 
            AND tx_date <= '2021-06-30' 
            AND ( transfer_date IS NULL OR transfer_date <= '2021-06-30' ) 
        ORDER BY
            tx_date ASC,
            units ASC 
        ) AS query1 
    WHERE
        tx_date < DATE_SUB( '2021-06-30', INTERVAL 1 YEAR ) 
        AND sum_units = 0 
    ORDER BY
        tx_date DESC 
        LIMIT 1 
    ) 
    AND tx_date <= '2021-06-30' 
    AND ( transfer_date IS NULL OR transfer_date <= '2021-06-30' ) 
ORDER BY
    tx_date ASC,
    units ASC


clarification
I have written a main query and in one of the where clauses I am using a subquery and this subquery works well, until this subquery does not return a result and my main query stops working, so I would like to on no result in this subquery substitute a value on no result so the main query can function normally

example needed
select * from table where date > (ifnull(subquery, "2002-01-01"))

I get
incorrect parameter count in the call to native function "IFNULL'


Solution 1:[1]

AND THE ANSWER IS: ---> :)
thanks guys for all your suggestions
much appreciated

SELECT *
        FROM trades 
        WHERE stock_code = 'IHVV' 
        AND acc_id = '4'
        AND tx_date > 
                    #last 0 date out of 1 year perhaps change the date range by from date and to date in the interval 1 year area
                (SELECT COALESCE(
                (SELECT tx_date 
                    FROM    (SELECT *, (@sum_units := @sum_units + units) AS sum_units  
                            FROM trades 
                            JOIN ( SELECT @sum_units := 0 ) params 
                            WHERE stock_code = 'ANZ' 
                                    AND acc_id = '4' 
                                    AND tx_date <= '2022-06-30' 
                                    AND (transfer_date IS NULL OR transfer_date <= '2022-06-30' ))
                            ORDER BY tx_date ASC, units ASC) as query1
                WHERE tx_date < DATE_SUB('2022-06-30',INTERVAL 1 YEAR) 
                        AND sum_units = 0
                ORDER BY tx_date DESC
                LIMIT 1), 'TODATE')         
        
            AND tx_date <= '2022-06-30' 
            AND (transfer_date IS NULL OR transfer_date <= '2022-06-30' )
    ORDER BY tx_date ASC, units ASC

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1