dqhdz04240 2015-02-24 20:27 采纳率: 0%
浏览 43
已采纳

Mysql限制选择不共享列的行

Using PHP MySQL, I'm storing in a DB a user's IP and the at my site that they entered on. The rows look like:

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

I'm trying to make a query that takes up to 300 IP's from a form and searches for where the users came from so I started with this (shortened to show only 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=%';

This gives me a result like:

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

Some IPs have 1,000+ entries because of proxies, bots etc so I can't use their info. If there is more than one "entrypoint" that contains "source=" for a given IP, I want to ignore that row. It also makes searching large numbers of IPs difficult because if I am searching for one of those IP's and return all of the rows associated with it, I'll run out of memory in PHP.

Instead of making PHP do the work of sorting out the results that share an IP, is there a way to write the query so that it won't return anything for an "ip" if there is more than one value in "entrypoint"? That is I want it to return:

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

If I ran it on just those 4 rows above using those 4 IP's. Since xxx.xxx.xxx.5 had two different entrypoints, I want to ignore those rows.

  • 写回答

1条回答 默认 最新

  • doulin3510 2015-02-24 20:50
    关注

    You should group by ip your result, count the number of distincts entrypoint, and returning the ones with 1 entrypoint

     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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么