Question

I need a way to query a collection of data. I have a list of recent activity dates each stored in 1 row per user. Each row has a field of loginDates which consists of a comma separated list of timestamps.

What i need to do is run reports on this date to find people active since XXXXXX timestamp. The problem is the fact it's comma separated means i can't query it uses methods i know.

Here is an example row

id  userID  accessDates
2   6   1399494405,1399494465,1399494525,1399494585,1399494623

What i want to achieve in plain text

SELECT all_fields FROM accessTable WHERE accessDate > YESTERDAY 

ALSO These dates may however span over several hundreds of days with hundreds of timestamps in the field.

Was it helpful?

Solution

Assuming the TimeStamp values are in order as your data sample shows, if any of the TimeStamp values in the string are greater than a given date, then the latest one would be greater than that value as well. So you only need the latest TimeStamp value to meet your requirement:

SET @Yesterday =
  UNIX_TIMESTAMP(DATE_ADD(DATE(CURRENT_TIMESTAMP()),INTERVAL -1 DAY));

SELECT *
FROM accessTable
WHERE  CAST(RIGHT(accessDates,10) AS UNSIGNED) > @Yesterday;

If you want to query each of those TimeStamps individually, the best solution is to put them into a single table column with a userid:

userID    accessDate
------    ----------
6         1399494405
6         1399494465
6         1399494525
6         1399494585
6         1399494623
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top