SQL 将 2 个结果合并为一个
SQL merge 2 result in one
事情是这样的:
我有两个 table。一个包含带有网站的客户,另一个 table 包含客户的可选网站。
我想知道2中是否有重复网站table
例如:
table一个:
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
...
我想要 tableA、tableB 和 tableAB 中的所有重复网站。嗯,全部重复..
我开始这样做了:
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
但是 UNION 语句花费了太多时间,显然,它遗漏了一些东西...
我也尝试用 OR 语句替换 UNION,但是如果 tableA 和 tableB 中有相同的网站,它不会取两个结果,而是一个。
我想要的输出是
网站
aaaa@aaa.a
dddd@ddd.d
请帮忙,
谢谢
编辑:
终于成功了:
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;
SQL 将 2 个结果合并为一个:
1.) use UNION ALL
执行查询时变慢的原因有很多。
1.) Machine specs.
2.) Database Structure.
3.) Indexes.
4.) Database Software it self.
事情是这样的: 我有两个 table。一个包含带有网站的客户,另一个 table 包含客户的可选网站。
我想知道2中是否有重复网站table
例如:
table一个:
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
...
我想要 tableA、tableB 和 tableAB 中的所有重复网站。嗯,全部重复..
我开始这样做了:
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
但是 UNION 语句花费了太多时间,显然,它遗漏了一些东西...
我也尝试用 OR 语句替换 UNION,但是如果 tableA 和 tableB 中有相同的网站,它不会取两个结果,而是一个。
我想要的输出是
网站
aaaa@aaa.a
dddd@ddd.d
请帮忙,
谢谢
编辑:
终于成功了:
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;
SQL 将 2 个结果合并为一个:
1.) use UNION ALL
执行查询时变慢的原因有很多。
1.) Machine specs.
2.) Database Structure.
3.) Indexes.
4.) Database Software it self.