我想加入两个表,但是要指定结果应该在其中一列上具有唯一值。
http://sqlfiddle.com/#!2/70ded/4
而不是
URLID DOMAINID
13 5
9 3
10 3
11 4
12 4
6 2
7 2
8 2
1 1
2 1
3 1
4 1
5 1
我想得到:
URLID DOMAINID
13 5
9 3
11 4
6 2
1 1
是否可以通过创建更快查询来实现?
答案 0 :(得分:1)
这个怎么样?我刚刚将汇总应用于urlid
并添加了group by
:
SELECT min(urlid) urlid, domain_stack.domainid
FROM domain_stack
INNER JOIN url_stack
ON url_stack.domainid = domain_stack.domainid
group by domain_stack.domainid
order by domain_stack.domainid desc
答案 1 :(得分:1)
您的查询速度不会更快,因为您必须删除重复项。这是一种MySQL方式:
select urlID, domainID
from (<your query>) as t
group by domainID
这使用称为隐藏列的MySQL mis(feature)。在大多数数据库中,您将使用:
select min(urlID), domainID
from (<your query>) as t
group by domainID
答案 2 :(得分:0)
SELECT urlid,domain_stack.domainid
FROM domain_stack
INNER JOIN url_stack
ON url_stack.domainid = domain_stack.domainid
GROUP BY domainid