Is it possible compare data from 2 tables in one table?
table 1
ID | Val |
---|---|
1 | Ann |
2 | Ben |
table 2
ID | Val |
---|---|
1 | Ann |
3 | Cyc |
output
ID | Val1 | Val2 | Match |
---|---|---|---|
1 | Ann | Ann | 1 |
2 | Ben | 0 | |
3 | Cyc | 0 |
UNION stacks queries on top, so one would end up with 4 rows (1,2,1,3). And INNER JOIN may not be the best solution since we do not know which table is longer?
(SELECT t1.id, t1.val, new.sum_val, t1.val = new.sum_val as Match
FROM t1
INNER JOIN
(SELECT t2.id, SUM(t2.val) as sum_val
FROM t2
WHERE t2.date = TODAY()
GROUP BY t2.ID) new on t1.ID = t2.ID
WHERE t1.date = TODAY())