繁体   English   中英

MySQL限制选择到不共享同一列的行

[英]Mysql limit select to rows that do not share a column in common

使用PHP MySQL,我将用户的IP和用户输入的IP存储在数据库中。 这些行看起来像:

ip              entrypoint
xxx.xxx.xxx.1   /index.php?source=google
xxx.xxx.xxx.5   /other.php?source=bing
xxx.xxx.xxx.5   /other.php?source=yahoo
xxx.xxx.xxx.4   /more.php

我正在尝试从一个表单中查询一个最多占用300个IP的查询,并搜索用户的来源,因此我从这里开始(简称为仅显示4个):

SELECT entrypoint,ip from TABLE WHERE ip IN('xxx.xxx.xxx.1','xxx.xxx.xxx.5','xxx.xxx.xxx.5','xxx.xxx.xxx.4') AND entrypoint LIKE '%source=%';

这给了我这样的结果:

ip              entrypoint
xxx.xxx.xxx.1   /index.php?source=google
xxx.xxx.xxx.5   /other.php?source=bing
xxx.xxx.xxx.5   /other.php?source=yahoo

由于代理,漫游器等原因,某些IP具有1,000多个条目,因此我无法使用其信息。 如果有多个“入口点”包含给定IP的“ source =”,我想忽略该行。 这也使搜索大量IP变得困难,因为如果我要搜索这些IP之一并返回与其关联的所有行,则PHP的内存将用完。

除了让PHP进行整理共享IP的结果的工作以外,还有一种方法可以编写查询,以便如果“ entrypoint”中有多个值,则该查询不会为“ ip”返回任何内容? 那就是我希望它返回:

xxx.xxx.xxx.1   /index.php?source=google

如果我使用这4个IP在上面的那4行上运行它。 由于xxx.xxx.xxx.5具有两个不同的入口点,因此我想忽略这些行。

您应该对结果进行ip分组,计算非重复入口点的数量,并返回带有1个入口点的

 SELECT entrypoint,ip, count(distinct entrypoint) nb
 from TABLE WHERE ip
      IN('xxx.xxx.xxx.1','xxx.xxx.xxx.5','xxx.xxx.xxx.5','xxx.xxx.xxx.4')
 AND entrypoint LIKE '%source=%' group by ip HAVING nb=1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM