Mysql 查询 - 一个 table - 在一天内查找两个日期 - 来自不同列的同一实体 + 差异计算
Mysql query - one table - find two dates within one day - of the same entity from different column + diff calculations
我正在尝试计算 mysql 查询来计算 table 包含办公室人员的来龙去脉。
我有:
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
我想得到什么:
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)
想法是获取一天中在办公室花费的时间信息。此外,我需要每个人整个月的会议记录摘要。每 person/per 个月分组。
我用了一段时间,用了这个查询,但是效果一般:
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;
感谢您的帮助!
亚当
您可以从这里开始:
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;
会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
查询将值在 IN
中的一行与同一个人的值在 OUT
中的下一行连接起来。这假设 id
是一个 AUTO_INCREMENT PRIMARY KEY 并且您的数据是正确的。
您现在可以将其更改为 GROUP BY 查询。
保罗为每天的会议记录提供的查询的改进是:
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;
飞蛾的查询如下所示:
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;
我正在尝试计算 mysql 查询来计算 table 包含办公室人员的来龙去脉。
我有:
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
我想得到什么:
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)
想法是获取一天中在办公室花费的时间信息。此外,我需要每个人整个月的会议记录摘要。每 person/per 个月分组。
我用了一段时间,用了这个查询,但是效果一般:
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;
感谢您的帮助! 亚当
您可以从这里开始:
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;
会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
查询将值在 IN
中的一行与同一个人的值在 OUT
中的下一行连接起来。这假设 id
是一个 AUTO_INCREMENT PRIMARY KEY 并且您的数据是正确的。
您现在可以将其更改为 GROUP BY 查询。
保罗为每天的会议记录提供的查询的改进是:
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;
飞蛾的查询如下所示:
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;