如果聚合在另一列上,则匹配具有一定计数的行

发布于 2024-12-06 20:17:55 字数 500 浏览 0 评论 0原文

我有一个包含以下字段的表:Url, IP, input

我想检索具有不同输入的不同 IP 的所有 URL(以及相应的 IP)

示例:

Url1, IP1, input1
Url2, IP2, input1
Url3, IP3, input1
Url1, IP4, input2
Url2, IP5, input2
Url3, IP3, input2
Url4, IP3, input2

我试图获取结果像这样:

Url1, IP1, input1
Url1, IP4, input2
Url2, IP2, input1
Url2, IP5, input2

由于Url3在不同的输入上获得相同的IP3,我也不希望它出现在结果中

,我不想要Url4,因为它只在input2中

我搜索子查询和联接(在同一个表上),但最后结束于外部脚本:有没有办法直接在 SQL 中完成

I have a single table with these fields: Url, IP, input

I would like to retrieve all Urls (and corresponding IPs) that have a different IP with different input

example:

Url1, IP1, input1
Url2, IP2, input1
Url3, IP3, input1
Url1, IP4, input2
Url2, IP5, input2
Url3, IP3, input2
Url4, IP3, input2

I was trying to get a result like this:

Url1, IP1, input1
Url1, IP4, input2
Url2, IP2, input1
Url2, IP5, input2

as Url3 gets the same IP3 on different inputs, I don't want it in the result

also, I do not want Url4 because it's only in input2

I've search for subqueries and joins (on same table), but finally ended doing it in an external script: is there a way to do it directly in SQL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我为君王 2024-12-13 20:17:55

尝试使用 GROUP BY、HAVING 和子选择:

create table your_table
(Url varchar(50),IP varchar(50), input varchar(50));


insert into your_table (Url, IP, input) values ('Url1', 'IP1', 'input1');
insert into your_table (Url, IP, input) values ('Url2', 'IP2', 'input1');
insert into your_table (Url, IP, input) values ('Url3', 'IP3', 'input1');
insert into your_table (Url, IP, input) values ('Url1', 'IP4', 'input2');
insert into your_table (Url, IP, input) values ('Url2', 'IP5', 'input2');
insert into your_table (Url, IP, input) values ('Url3', 'IP3', 'input2');



select *
from your_table
where (Url,IP) in
(
select Url, IP
from
(
select Url, IP, count(*)
from your_table
group by Url, IP
having count(*) = 1
) a
);

Try using GROUP BY, HAVING and sub selects:

create table your_table
(Url varchar(50),IP varchar(50), input varchar(50));


insert into your_table (Url, IP, input) values ('Url1', 'IP1', 'input1');
insert into your_table (Url, IP, input) values ('Url2', 'IP2', 'input1');
insert into your_table (Url, IP, input) values ('Url3', 'IP3', 'input1');
insert into your_table (Url, IP, input) values ('Url1', 'IP4', 'input2');
insert into your_table (Url, IP, input) values ('Url2', 'IP5', 'input2');
insert into your_table (Url, IP, input) values ('Url3', 'IP3', 'input2');



select *
from your_table
where (Url,IP) in
(
select Url, IP
from
(
select Url, IP, count(*)
from your_table
group by Url, IP
having count(*) = 1
) a
);
终遇你 2024-12-13 20:17:55

GROUP BY 就可以了

SELECT MIN(Url), IP, input
FROM   YourTable
GROUP BY
       IP, input

A GROUP BY would do

SELECT MIN(Url), IP, input
FROM   YourTable
GROUP BY
       IP, input
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文