i have two tables:
Table 1
x
id | column_1 | column_2 | parent_id | added_at
-------------------------------------------------
1 | 0 |0 | 1 | Date 1
2 | 0 |0 | 1 | Date 2
3 | 1 |0 | 1 | Date 3
4 | 1 |0 | 1 | Date 4
Table 2
id | column_1 | column_2 | parent_id | added_at
-------------------------------------------------
1 | 0 |0 | 1 | Date 5
2 | 0 |0 | 1 | Date 6
3 | 1 |0 | 1 | Date 7
4 | 1 |0 | 1 | Date 8
The result should be for this particular case:
[[2], [4], [1], [2], [3], [4]]
we should get the newest ids when column_1 and column_2 are duplicated in both tables.
i’ve been trying with something like this one:
SELECT x.id
FROM (
SELECT t1.column_1, t1.column_2, t1.parent_id, t1.id
FROM table_1 t1
UNION
SELECT t2.column_1, t2.column_2, t2.parent_id, t2.id
FROM table_2 t2
) x
GROUP BY x.position_x, x.position_y, x.parent_id
But i’m getting [[1], [3]]
Expected result with ids from both tables:
[[2], [4], [1], [2], [3], [4]]
2, 4
are ids from the first table and 1, 2, 3, 4
are ids from the second table
Advertisement
Answer
may be this is what you want :
SELECT MAX(id) as last_id
FROM table_1
GROUP BY column_1, column_2
UNION
SELECT id
FROM table_2