Please refer to the example query below::
SELECT
a.id,
a.status,
a.updated_at,
b.id,
b.updated_at,
IF((a.status = 1 OR a.status = 2),
(IF(a.status = 1),
(SELECT COUNT(*) FROM coconut c WHERE c.datetime BETWEEN a.updated_at AND b.updated_at) ,
(SELECT COUNT(*) FROM coconut c WHERE c.datetime BETWEEN a.updated_at AND NOW()) ), 0) AS 'ab'
FROM apple a
LEFT JOIN banana b
The sub-queries inside IF condition are the issue that gave me error message: "unknown column a.updated_at" and "unknown column b.updated_at" when I'm trying to get the data from table apple and table banana.
Is there any other way to pass the value from table apple and table banana into the sub-query?