I want to use sql JOIN with COUNT in a way that using the COUNT as the condition for the JOIN.
e.g.
SELECT * FROM tbl1
INNER JOIN (SELECT * FROM tbl2) t2
ON (SELECT COUNT(*) FROM tbl1) > 0
Is this possible or can someone please tell me another way?
PS- The real question I’m having is that I have two tables A and B and I need to select id from A where the date of creation is greater than some value. At the same time I want to make sure that table B does not have that id in table A. This works fine but when I have no data in table B, the query gives no result.
SELECT a.user_id from (SELECT * FROM A WHERE DATEDIFF(event_date,'certain_value') >=another_value) a INNER JOIN B b ON a.user_id != b.user_id
I tried to use count like this but failed
SELECT a.user_id from (SELECT * FROM A WHERE DATEDIFF(event_date,'certain_value') >=another_value) a INNER JOIN B b ON count(B.user_id) = 0 OR a.user_id != b.user_id
The way your query is written, condition in
ONis doing nothing and equivalent toON (1=1)(if there is no records in tbl1,select * from tbl1 inner join tbl2 on [any condition]will always return empty resultset).From your updated question I think you need: