php - 连接表并计算两个日期

标签 php mysql

我很难理解如何连接表,很抱歉我的问题 =D

好的。我有两个表:usersuser_traning

user_traning 使用以下列添加用户训练:

id, user_id, date, type, notes

users 表使用上次登录日期时间更新 user_last_login 行。

我的想法是在 user_traning.date 的帮助下计算 users.user_last_login 之间的 user_training 中有多少(新)行.

我一直在这样尝试:

    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()

所以我可以在页面上显示一个小徽章,自上次访问以来添加了多少个新 session =)

非常感谢您的帮助!

最佳答案

你几乎在那里,但你最后有一个小语法问题,它应该是

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()

更新:来自评论中提供的解释。

考虑以下两个表

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 |
+---------+------+---------------------+

从上面的示例数据中,user_training 表中有 4 个条目发生在上次登录 user_id = 2

之后

现在我们可以得到这些数据了

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  ;

这会给你

+------+---------+---------------------+---------+------+---------------------+---------+---------------------+
| 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 |
+------+---------+---------------------+---------+------+---------------------+---------+---------------------+

因此,为了获得计数,我们可以在查询中使用 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 

关于php - 连接表并计算两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29623507/

相关文章:

php - 如何使用 codeigniter uri 格式创建搜索表单?

php - Drupal 忽略服务器权限并显示权限错误

javascript - 将 Google 转换代码添加到 WordPress Contact Form 7

mysql - 我不能在mysql中使用 "like"和 "in"吗?

MySQL:如果找不到表名,则从架构中删除特定表?

php - 根据键拆分关联数组

java - Android应用程序和php之间发送和接收数据的问题

mysql - MariaDb 通过 Puppet 和 Vagrant

javascript - 如何从动态创建的按钮获取属性?

mysql - JPQL 中的成员未转换为 SQL 存在子句