我有一个包含排名信息的信息表,例如
Id | Keyword | Url |
=======================================================
1 | google | https://www.google.co.uk/ |
2 | google | https://maps.google.co.uk/ |
3 | google | https://www.facebook.com/Google/ |
4 | google | https://www.youtube.com/user/Google |
5 | pizza | https://www.pizzahut.co.uk/ |
6 | pizza | https://www.dominos.co.uk/ |
7 | pizza | https://www.just-eat.co.uk/ |
8 | pizza | https://www.yell.com |
我需要能够找到所有不对特定网址排名的关键字,因此在上表中,如果我要查找排名为https://www.google.co.uk/的关键字,则只返回一个结果的比萨饼
谢谢,
詹姆斯
答案 0 :(得分:2)
如果您想要给定URL
不存在的关键字,可以使用以下方法之一:
select keyword
from t
group by keyword
having sum(case when url = $url) = 0;
或者,如果您有单独的关键字列表:
select kw.keyword
from keywords kw
where not exists (select 1 from t where t.url = $url and t.keyword = kw.keyword);
您还可以使用子查询实现此方法:
select kw.keyword
from (select distinct keyword from t) kw
where not exists (select 1 from t where t.url = $url and t.keyword = kw.keyword);
答案 1 :(得分:0)
如果您想要退回任何一条pizza
记录,可以尝试使用
SELECT * FROM table WHERE LOWER(Url) NOT LIKE 'google' LIMIT 1
如果您想要的所有结果都不匹配google
,则只需删除LIMIT
SELECT * FROM table WHERE LOWER(Url) NOT LIKE 'google'