MySQL Query Algorithm - I am overwhelmed

梦想的初衷 提交于 2020-05-28 07:28:27

问题


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.


回答1:


Here's a SQLFiddle for your schema with the query.

Schema

CREATE TABLE members (
  id INT NOT NULL PRIMARY KEY,
  name VARCHAR(20),
  surname VARCHAR(20)
  );

CREATE TABLE logs (
  member_id INT NOT NULL,
  ip_address VARCHAR(15)
  );

INSERT INTO members VALUES
( 87, "John", "But"),
( 88, "John", "But"),
( 89, "Alex", "Lopez"),
( 90, "David", "Beckham");

INSERT INTO logs VALUES
( 87, "1.1.1.1"),
( 88, "1.1.1.1"),
( 89, "2.2.2.2"),
( 90, "2.2.2.2");

Query

SELECT ip_address,
  GROUP_CONCAT(member_id) as member_ids, 
  count(*) as total 
FROM logs
WHERE ip_address = "2.2.2.2"
GROUP BY ip_address;

And results

ip_address  member_ids  total
2.2.2.2     89,90       2



回答2:


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.



来源:https://stackoverflow.com/questions/61284619/mysql-query-algorithm-i-am-overwhelmed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!