简体   繁体   English

联接表并在两个日期之间计数

[英]Join tables and count between two dates

Im having a hard time understanding how to join tables, so sorry for my question =D 我很难理解如何联接表,对不起我的问题= D

Alright. 好的。 I have two tables: users and user_traning . 我有两个表: usersuser_traning

user_traning adds users training with the following columns: user_traning使用以下列添加用户培训:

id, user_id, date, type, notes etc id, user_id, date, type, notes

The users tables updates the user_last_login row with the last login datetime. users表使用上次登录日期时间更新user_last_login行。

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 . 我的想法是在user_training帮助下, user_traning.date user_training之间的users.user_last_login有多少行(新)。

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 从上面的样本数据中,在user_training表中有4个条目,发生在上次登录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 因此,要获取计数,我们可以仅使用查询中的count作为

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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