Reputation: 3
I have a sql qustion. its suppose to be simple but i cant figure it out. I have 2 tables:
**customer1 -**
id name
1 aaa
2 bbb
3 ccc
4 ddd
**customer2**-
id name
1 aaa
2 bbb
5 eee
6 fff
I need to get ALL the customers in both tables. i need to query in a way that if a customer is on both tables I will see him twice in the results (e.g aaa, bbb), and all the rest only one.
Thanks a lot.
Upvotes: 0
Views: 32
Reputation: 35333
I would have put this as a comment to : Raghavendra Kedlaya post; but it wouldn't let me.
I like to know from whence the data came when using union all..
SELECT id, name, 'Customer1' as src
FROM customer1
UNION ALL
SELECT id, name, 'Customer2' as src
FROM customer2
Upvotes: 0
Reputation: 881
Pay attention to UNION and UNION ALL , with the first one you will not get duplicate , with the second one you will get even duplicate as result of your query.
Let's talk about the performance : A UNION is highly optimized and really fast, except in cases where one query finishes long before the other, and you have to wait to get the whole result set.
When you don't mind about duplicates , UNION ALL will be faster.
Upvotes: 1
Reputation: 116
select id, name from customer1
union all
select id, name from customer2
Upvotes: 2