Вопрос

I have a (my)sql table which holds the following data about gym visits:

gym_visits (
  id INT
  customer_id INT
  checkin_time DATETIME
)

Each customer has a personal card which they use to check in to the gym. I would like to know if there are people visiting this gym together on multiple occasions (friends who visit the gym together) and create a social graph. I think a good criteria would be if they'd check in at most 5 minutes from each other on multiple (lets say 3) occasions.

The easiest way would be to loop through all the records with for example php and look for all other records within that time span and create an adjacency matrix, but that would take a lot of calculating power and time. There should be a better way to calculate it, but I can't think of any. Do you know one?

Это было полезно?

Решение

To find out if a customer has visited more than once in a day you could do:

SELECT customer_id, count(*) as numberOfVisits
FROM gym_visits
WHERE checkin_time = <some date value>
GROUP BY customer_id

To find out possible friends of customers who came into the gym within a certain time interval without looping through the table per customer requires a different strategy. One way to do it would be as follows:

CREATE TABLE friends
(
    customer_id int,
    friend_id int
    // add other columns as you see fit
)
  1. Create another table with the above structure:

  2. Create an insert trigger on the gym_visits table. When a new customer arrives at time NOW(), the trigger will find all the customers in the gym_visits table who have arrived between NOW() - X minutes (X being any interval/rolling time window you choose). This trigger will then insert a row for all customers it has found in that interval (ie possible friends) into the friends table for that new customer. This way you easily capture possible friendships and the table contains all the details to allow you to produce a social graph of possible friendships.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top