S
S
sbh2020-05-01 16:22:17
MySQL
sbh, 2020-05-01 16:22:17

How to do JOIN with BETWEEN condition?

There is 1 temporary table, contains sessions and their time:
id session_start session_end
132123 2020-04-29 11:02:02 2020-04-29 12:01:02
132123 2020-04-29 16:03:02 2020-05- 01 20:02:22
...

there are 2 tables - it contains queries with timestamps:
client query timestamp
132123 first_data 2020-04-29 16:03:32
321231 second_data 2020-04-29 16:03:33
312332 first_data 2020 -04-29 16:03:33
132123 third_data 2020-04-30 17:03:32
... It is

necessary to actually select records from 2 tables that satisfy the conditions:
1) client = id
2) the timestamp falls within the session period.

I think it is necessary to use JOIN here, but the correct request cannot be made.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-05-01
@Rsa97

SELECT *
  FROM `t1`
  JOIN `t2` ON `t2`.`client` = `t1`.`id`
    AND `t2`.`tiimestamp` BETWEEN `t1`.`session_start` AND `t1`.`session_end`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question