mysql 查询算法,MySQL查询算法-我不知所措

I have 2 different tables.

My goal is to find people who use the same ip address with different (name and surname).

Table 2 - members

id name surname

87 john but

88 john but

89 alex lopez

90 david beckham

Table 1 - logs

member_id ip_adress

87 1.1.1.1

88 1.1.1.1

89 2.2.2.2

90 2.2.2.2

The result I want to get

ip_address members count

2.2.2.2 89,90 total (2 )

I have been trying for days, we could not solve.

As a result, I need to get the above output.

解决方案

Welcome to S/O, and you have a good question, confusing several, but you did give a good example that you did not want an IP if it was the same name / surname. In order to get what you are looking for, you need to self-join to the same logs table, but also the same members tables so you have the capacity of matching on IP first, then also comparing the name/surname of each person so they can be excluded if need be.

Its not perfect, but this does get what you are looking for from the sample data without specifically filtering on one IP address.

select

l1.ip_address,

group_concat(distinct l1.member_id order by l1.member_id ) UniqueMembers,

count(*) NumberOfMembers

from

logs l1

JOIN members m1

on l1.member_id = m1.id

join logs l2

on l1.ip_address = l2.ip_address

AND l1.member_id != l2.member_id

join members m2

on l2.member_id = m2.id

AND m1.name != m2.name

AND m1.surname != m2.surname

group by

l1.ip_address

I am using l1 and l2 for the respective "Logs" aliases and m1 and m2 for "members". So I am starting on the same member ID from l1 to m1. Then from first log table to second based on the IP address and must be a different member ID (why compare same person by same member ID). Now I can join l2 and m2 by its member ID and exclude it if the m1 and m2 names match because qualifying for != on the name and surname.

Should get you much closer to what you are looking for.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值