Thibault Beziers La Fosse Thibault Beziers La Fosse - 2 months ago 5
MySQL Question

SQL merge 2 result in one

here the thing :
I have two table. One contains customer with website and another table contain optionnal website of the customer.

I want to know if there is dupplicate website in the 2 table

For example :

tableA :

cust_id cust_website
1 aaaa@aaa.a
2 bbbb@bbb.b
3 cccc@ccc.c
4 aaaa@aaa.a
5 dddd@ddd.d
...


tableB



cust_id cust_optionnalWebsite
3 uuuu@uuu.u
4 dddd@ddd.d
...


I would like to have all dupplicate website in tableA, table B and tableAB. Well, all dupplicate..

I started to do like this :

SELECT cust_website FROM tableA WHERE cust_website IN (SELECT cust_optionnalWebsite FROM tableB UNION SELECT cust_website FROM tableA) GROUP BY cust_website HAVING COUNT(cust_website) > 1


but UNION statement takes sooo much time, and obviously, it's missing something...

I also tried replace UNION by OR statement, but if there is the same website in tableA and tableB, it doesn't take two result but just one.

the output that I want is

website

aaaa@aaa.a

dddd@ddd.d

please help,

thank you

EDIT :

Finally this works :

SELECT website FROM ( SELECT cust_optionnalWebsite as website FROM tableB UNION ALL SELECT cust_website as website FROM tableA ) GROUP BY website HAVING(website)>1;

Answer

SQL merge 2 result in one:

1.) use UNION ALL

There's a lot of causes why you are getting slow when executing a query.

1.) Machine specs.
2.) Database Structure.
3.) Indexes.
4.) Database Software it self.
Comments