I've read at least 10 SO questions on this topic and am not able to figure this out. I'm working in Jupyter, but I don't think it makes a difference.
I have two tables:
Table A Table B
+-----+-----+ +-----+-----+
| x | y | | x | y |
+-----+-----+ +-----+-----+
| 1 | 2 | | 1 | 3 |
| 2 | 3 | | 4 | 6 |
| 3 | 4 | | 7 | 9 |
| 4 | 5 | | 10 | 12 |
| 5 | 6 | +-----+-----+
+-----+-----+
I'm able to take the union of these tables with the following query:
SELECT * FROM (
SELECT * FROM A UNION SELECT * FROM B
)
However, I'm not sure how to go about returning the four rows that have duplicate x
columns from this union. I've tried GROUP BY x HAVING COUNT(*) > 1
, but this only returns 2 rows. The output should be:
+-----+-----+
| x | y |
+-----+-----+
| 1 | 2 |
| 1 | 3 |
| 4 | 5 |
| 4 | 6 |
+-----+-----+
You seem to want:
select a.*
from a
where exists (select 1 from b where b.x = a.x)
union all
select b.*
from b
where exists (select 1 from a where a.x = b.x);
union
incurs overhead for removing duplicates. This seems unnecessary in your case. So, if you don't need duplicate removal, use union all
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.