Thom Thom - 22 days ago 8
MySQL Question

UNION with cross table where clause

(SELECT *, 0 AS user FROM table1)
UNION
(SELECT * FROM table2 WHERE unix >= {$threemonths})
ORDER BY unix DESC;


I need to add:

WHERE table2.identifier = table1.identifier
or something

I want to get all from
table1
and only rows from
table2
where
identifier
is found in the results from
table1
's
identifier
column.

Answer

Please see if this works for you

(SELECT *, 0 AS user FROM table1) 
UNION 
(SELECT * FROM table2 WHERE unix >= {$threemonths} and exists (select 'Y' from table1 a where a.identifier = table2.identifier)) 
ORDER BY unix DESC;
Comments