gpt4 book ai didi

php - MySQL,按两个不同的列分组

转载 作者:可可西里 更新时间:2023-11-01 08:27:42 25 4
gpt4 key购买 nike

我的 table 看起来像:

╔═════╦═════════════════════╦═════════════════════╦═════════════╗
║ id ║ begin ║ end ║ employeesId ║
╠═════╬═════════════════════╬═════════════════════╬═════════════╣
║ 4 ║ 2015-11-11 00:00:00 ║ 2015-11-11 09:00:00 ║ 8 ║
║ 80 ║ 2015-11-11 09:00:00 ║ 2015-11-11 12:00:00 ║ 8 ║
║ 49 ║ 2015-11-11 00:00:00 ║ 2015-11-11 08:00:00 ║ 61 ║
║ 32 ║ 2015-11-11 08:00:00 ║ 2015-11-12 06:00:00 ║ 61 ║
║ 42 ║ 2015-11-12 07:00:00 ║ 2015-11-12 13:00:00 ║ 61 ║
║ 17 ║ 2015-11-11 00:00:00 ║ 2015-11-11 08:00:00 ║ 22 ║
║ 42 ║ 2015-11-11 09:00:00 ║ 2015-11-11 12:00:00 ║ 22 ║
╚═════╩═════════════════════╩═════════════════════╩═════════════╝

我想合并列 employeesId 相等的行,并且一行的 begin 时间等于另一行的 end 时间。像这样:

╔═════════════════════╦═════════════════════╦═════════════╗
║ begin ║ end ║ employeesId ║
╠═════════════════════╬═════════════════════╬═════════════╣
║ 2015-11-11 00:00:00 ║ 2015-11-11 12:00:00 ║ 8 ║
║ 2015-11-11 00:00:00 ║ 2015-11-12 06:00:00 ║ 61 ║
║ 2015-11-12 07:00:00 ║ 2015-11-12 13:00:00 ║ 61 ║
║ 2015-11-11 00:00:00 ║ 2015-11-11 08:00:00 ║ 22 ║
║ 2015-11-11 09:00:00 ║ 2015-11-11 12:00:00 ║ 22 ║
╚═════════════════════╩═════════════════════╩═════════════╝

编辑:

I need to merge continuous datetimes, in this example you can see that end time '2015-11-11 12:00:00' for employeesId = 8 replaced end time '2015-11-11 09:00:00' in first row because begin of the second row is equal to end of the first row.

最佳答案

尝试加入

SELECT a.`begin_date`, IF(b.`end_date` IS NOT NULL, b.`end_date`, a.`end_date`), a.`employer_id`
FROM `mytable` a
LEFT JOIN `mytable` b ON a.`employer_id` = b.`employer_id` AND a.`end_date` = b.`begin_date`
LEFT JOIN `mytable` c ON a.`employer_id` = c.`employer_id` AND a.`begin_date` = c.`end_date`
WHERE c.`id` IS NULL

但是你应该重建你的架构。

关于php - MySQL,按两个不同的列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33668982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com