简体   繁体   中英

Join tables and count between two dates

Im having a hard time understanding how to join tables, so sorry for my question =D

Alright. I have two tables: users and user_traning .

user_traning adds users training with the following columns:

id, user_id, date, type, notes etc

The users tables updates the user_last_login row with the last login datetime.

My thought is to count how many (new) rows there is in user_training between users.user_last_login , with help from user_traning.date .

I have been trying like this:

    SELECT COUNT(*) FROM user_traning
    JOIN users
    on users.user_id = user_traning.user_id
    WHERE users.user_id = '2' and user_traning.user_id ='2' between 
users.user_last_login and now()

So i can show a little badge on the page, how many new sessions that has been addad since last visit =)

Help is much appreciated!

You are alomost there but you have a small syntax issue at the end, it should be as

SELECT COUNT(*) FROM user_traning ut
JOIN users u
on u.user_id = ut.user_id
WHERE u.user_id = '2' 
and ut.user_id ='2' 
and ut.date between u.user_last_login and now()

UPDATE : From the explanation provided in the comment.

Consider the following 2 tables

mysql> select * from user_training ;
+------+---------+---------------------+
| id   | user_id | date                |
+------+---------+---------------------+
|    1 |       1 | 2015-04-14 16:35:01 |
|    2 |       1 | 2015-04-14 16:35:10 |
|    3 |       3 | 2015-04-14 16:35:18 |
|    4 |       4 | 2015-04-14 16:35:25 |
|    5 |       2 | 2015-04-13 16:37:16 |
|    6 |       2 | 2015-04-14 15:57:26 |
|    7 |       3 | 2015-04-14 15:27:38 |
+------+---------+---------------------+
7 rows in set (0.00 sec)

mysql> select * from users ;
+---------+------+---------------------+
| user_id | name | user_last_login     |
+---------+------+---------------------+
|       1 | AA   | 2015-04-14 16:34:10 |
|       2 | BB   | 2015-04-14 16:34:10 |
+---------+------+---------------------+

From the above sample data there are 4 entry in the user_training table which happened after last login of user_id = 2

Now we can get those data as

select * from user_training ut 
left join users u on u.user_id = ut.user_id 
join ( 
   select user_id,user_last_login from users where user_id = 2 
)x on ut.date >=  x.user_last_login  ;

This will give you

+------+---------+---------------------+---------+------+---------------------+---------+---------------------+
| id   | user_id | date                | user_id | name | user_last_login     | user_id | user_last_login     |
+------+---------+---------------------+---------+------+---------------------+---------+---------------------+
|    1 |       1 | 2015-04-14 16:35:01 |       1 | AA   | 2015-04-14 16:34:10 |       2 | 2015-04-14 16:34:10 |
|    2 |       1 | 2015-04-14 16:35:10 |       1 | AA   | 2015-04-14 16:34:10 |       2 | 2015-04-14 16:34:10 |
|    3 |       3 | 2015-04-14 16:35:18 |    NULL | NULL | NULL                |       2 | 2015-04-14 16:34:10 |
|    4 |       4 | 2015-04-14 16:35:25 |    NULL | NULL | NULL                |       2 | 2015-04-14 16:34:10 |
+------+---------+---------------------+---------+------+---------------------+---------+---------------------+

So to get the count we can just use the count in the query as

select count(*) from user_training ut 
left join users u on u.user_id = ut.user_id 
join ( 
  select user_id,user_last_login from users where user_id = 2 
)x on ut.date >=  x.user_last_login 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM