简体   繁体   English

MySQL查询-一个表-一天之内找到两个日期-来自不同列+差异计算的同一实体

[英]Mysql query - one table - find two dates within one day - of the same entity from different column + diff calculations

I'm trying to figure a mysql query to do calculations of a table containing ins and outs of people in the office. 我正在尝试构建一个mysql查询来计算包含办公室人员来龙去脉的表格。

What I have: 我有的:

id  PERSON      IN                  OUT
1   Person A    2019-03-11 08:59:30 NULL
2   Person B    2019-03-11 08:32:00 NULL
3   Person C    2019-03-11 08:04:40 NULL
4   Person D    2019-03-11 07:58:50 NULL
5   Person E    2019-03-11 07:35:20 NULL
6   Person F    2019-03-11 07:35:00 NULL
7   Person A    NULL                2019-03-11 15:00:50
8   Person B    NULL                2019-03-11 14:57:00
8   Person C    NULL                2019-03-11 13:19:50
9   Person D    NULL                2019-03-11 15:14:20
10  Person E    NULL                2019-03-11 15:15:50
11  Person F    NULL                2019-03-11 15:28:10

What I would like to get: 我想得到什么:

id  PERSON      IN                  OUT                 DIFF IN MINUTES
1   Person A    2019-03-11 08:59:30 2019-03-11 15:00:50 XXX
2   Person B    2019-03-11 08:32:00 2019-03-11 14:57:00 XXX
3   Person C    2019-03-11 08:04:40 2019-03-11 13:19:50 XXX
4   Person D    2019-03-11 07:58:50 2019-03-11 15:14:20 XXX
5   Person E    2019-03-11 07:35:20 2019-03-11 15:15:50 XXX
6   Person F    2019-03-11 07:35:00 2019-03-11 15:28:10 XXX
                                                        TOTAL OF XXXS
                                                        TOTAL OF XXXS - YYY (constant)

The idea is to get the information of the time spent in the office during one day. 这个想法是获取在一天内在办公室花费的时间的信息。 Moreover I need summary of minutes from the whole month per person. 此外,我需要每人整个月的会议记录摘要。 Grouping per person/per month. 按人/每月分组。

I have spent some time and I use this query, but the effect is mediocre: 我花了一些时间来使用这个查询,但效果是平庸的:

SELECT a.PERSON, a.IN, b.OUT, TIMESTAMPDIFF(SECOND,a.IN,b.OUT)-28880 AS WHATSLEFT
FROM presence a
INNER JOIN presence b
ON a.PERSON = b.PERSON
WHERE DATEDIFF(a.IN,b.OUT) = 0 AND b.PERSON ="Person A"
ORDER BY a.IN;

Thanks for help! 感谢帮助! Adam 亚当

You can start from here: 你可以从这里开始:

SELECT a.PERSON, a.IN, b.OUT, TIMESTAMPDIFF(SECOND, a.IN, b.OUT) AS TOTAL
FROM presence a
LEFT JOIN presence b ON b.id = (
  SELECT MIN(b2.id)
  FROM presence b2
  WHERE b2.PERSON = a.person
    AND b2.id > a.id
    AND b2.OUT IS NOT NULL
)
WHERE a.IN IS NOT NULL
ORDER BY a.IN;

It will return: 它将返回:

PERSON      IN                      OUT                     TOTAL
-----------------------------------------------------------------
Person F    2019-03-11 07:35:00     2019-03-11 15:28:10     28390
Person E    2019-03-11 07:35:20     2019-03-11 15:15:50     27630
Person D    2019-03-11 07:58:50     2019-03-11 15:14:20     26130
Person C    2019-03-11 08:04:40     2019-03-11 13:19:50     18910
Person B    2019-03-11 08:32:00     2019-03-11 14:57:00     23100
Person A    2019-03-11 08:59:30     2019-03-11 15:00:50     21680

Demo 演示

The query joins a row which has a value in IN with the next row from the same person which has a value in OUT . 该查询将具有IN值的行与来自具有OUT值的同一个人的下一行连接。 This assumes that id is an AUTO_INCREMENT PRIMARY KEY and your data is correct. 这假设id是AUTO_INCREMENT PRIMARY KEY并且您的数据是正确的。

You can now change it to a GROUP BY query. 您现在可以将其更改为GROUP BY查询。

An improvement of the query provided by paul for the minutes each day would be: 保罗每天为分钟提供的查询的改进将是:

SELECT 
    a.PERSON, 
    a.IN, 
    b.OUT, 
    TIMESTAMPDIFF(MINUTE,a.IN,b.OUT) AS `DIFF IN MINUTES`
FROM presence a
INNER JOIN presence b ON ( a.PERSON = b.PERSON )
WHERE DAY(a.IN) = DAY(b.OUT)
ORDER BY a.IN;

DEMO DAY 演示日

The query for the moth would look like this: 蛾的查询如下所示:

SELECT 
    a.PERSON, SUM( TIMESTAMPDIFF(MINUTE,a.IN,b.OUT) ) AS `MINUTES`
FROM presence a
INNER JOIN presence b ON ( a.PERSON = b.PERSON )
WHERE DAY(a.IN) = DAY(b.OUT)
AND a.PERSON = 'Person A'
AND a.IN BETWEEN '2019-03-01' AND '2019-04-01' 
AND b.OUT BETWEEN '2019-03-01' AND '2019-04-01' 
GROUP BY a.PERSON;

DEMO MONTH 演示月份

暂无
暂无

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

相关问题 我如何在MySQL中查询两个日期,其中datefrom在同一表的第二列中? - How do I query two dates in mysql where the datefrom is in one column and dateto is in the second column of the same table? mysql在where子句中从同一表中选择两次具有不同日期的列 - mysql Select one column twice from the same table with different dates in the where clause 2个SQL查询中来自同一列的日期 - 2 Dates from the same column in one SQL query MySQL查询以查找一个表中一列的值是否在另一表中两列的两个值之间 - MySQL query to find if a value of one column in one table is between two values in two columns on another table 将一列从一个表复制到mysql中的另一个表(两个不同的数据库) - Copy a column from one table to another in mysql (two different databases) mysql在一个查询中具有不同记录的两个表 - mysql two table with different record in one query MySQL:在一个查询中根据不同的日期返回两组结果 - MySQL: Return two sets of results based on different dates in one query 如何使来自同一表的两个$ db-> query具有按列彼此独立地位于一个php页面中的两个不同ORDER? - How to make two $db->query from the same table with two different ORDER by column that each other stand independenty in one php page? 如何在同一表格的两个不同列中查找和显示相似单词的数量(MySql,PHP) - How to find and show number of similar words in two different column within same table(MySql,PHP) MYSQL-选择一个不同的值,并从同一表中选择一个相同的值 - MYSQL - Selecting one different value, and one the same from same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM