0

I have a table with user_id, session_start and session_end values.

I have a list of times in a file. For example, the list will look like the following:

2015-08-20 00:00:00
2015-08-20 00:15:00
2015-08-20 00:30:00
2015-08-20 00:45:00

So want to find all users who are logged in between that time and 15 minutes later, so all users logged in between (2015-08-20 00:00:00 and 2015-08-20 00:15:00, etc., etc.)

Session table looks like the following:

user_id    ----      session_start    ----     session_end
123               2015-08-20 00:01:00     2015-08-20 00:14:49
456               2015-08-20 00:17:31     2015-08-20 00:29:30

Query assistance would be quite helpful.

user3299633
  • 2,971
  • 3
  • 24
  • 38

3 Answers3

0

Query:

SELECT user_id 
FROM table 
WHERE '$time' >= session_start 
AND '$time' <= session_end;

where table is the name of your table, and $time is one line value from your file in DATETIME format (YYYY-MM-DD HH:MM:SS)

You can loop through each line of your file like:

$handle = fopen("inputfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
    }

    fclose($handle);
} else {
    // error opening the file.
} 

Second example copied from How to read a file line by line in php

Community
  • 1
  • 1
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
0

Something like this

select user_id from mytable where session_start <= '2015-08-20 00:15:00' 
    AND session_end >= '2015-08-20 00:00:00'

This is saying give me records where: The user started the session before the ending time and was still logged in after the beginning time.

mseifert
  • 5,390
  • 9
  • 38
  • 100
0

If you're only looking for people who actually logged in during that period, you can use this query:

select * from session
where session_start >= '2015-08-20 00:00:00'
    and session_start < '2015-08-20 00:15:00';

If you're looking for people who were connected over that period, use this one:

select * from session
where session_start <= '2015-08-20 00:15:00'
    and session_end >= '2015-08-20 00:00:00';
Phil Cairns
  • 758
  • 4
  • 15