我有存储日期时间的表...我需要使用mysql来计算0d 0h 0m 0s格式的持续时间以在数据表中显示它:
+----+---------------------+
| id | playtime |
+----+---------------------+
| 2 | 08:58:24 20/06/2017 |
| 1 | 08:57:33 20/06/2017 |
+----+---------------------+
这是我的疑问:
SELECT (SELECT GROUP_CONCAT(
TIMESTAMPDIFF(day, live.playtime, NOW()) , 'd ',
MOD(TIMESTAMPDIFF(hour, live.playtime, NOW()), 24), 'h ',
MOD(TIMESTAMPDIFF(minute, live.playtime, NOW()), 60), 'm ',
MOD(TIMESTAMPDIFF(second, live.playtime, NOW()), 60), 's'
) FROM live) AS duration;
问题是只计算第一个id而忽略其他id。如何为每个id计算时间返回多个结果?
我需要回复:
+-----------------------------+
| duration |
+-----------------------------+
| 0d 0h 13m 50s,0d 0h 12m 59s |
+-----------------------------+
| 0d 0h 11m 12s,0d 0h 6m 9s |
+-----------------------------+
更新完整查询:
SELECT SQL_CALC_FOUND_ROWS live.id, live.user, live.player,
SUBSTRING_INDEX(streams.channel, '_', -1) AS channel, bouquets.bouquet,
DATE_FORMAT(live.playtime, '%H:%i:%s %d/%m/%Y') AS playtime,
(SELECT CONCAT(
TIMESTAMPDIFF(day, live.playtime, NOW()) , 'd ',
MOD(TIMESTAMPDIFF(hour, live.playtime, NOW()), 24), 'h ',
MOD(TIMESTAMPDIFF(minute, live.playtime, NOW()), 60), 'm ',
MOD(TIMESTAMPDIFF(second, live.playtime, NOW()), 60), 's'
) FROM live GROUP BY live.id LIMIT 1) AS duration,
resellers.nick, live.ip, servers.server, live.id, live.id
FROM live LEFT JOIN streams ON live.stream=streams.id LEFT JOIN bouquets ON
live.bouquet=bouquets.id LEFT JOIN resellers ON live.reseller=resellers.id
LEFT JOIN servers ON live.server=servers.id
ORDER BY live.id desc LIMIT 0, 10;
答案 0 :(得分:1)
假设每个ID只有一条记录,您是否可以尝试在嵌套WHERE
查询中添加SELECT
子句,例如:
SELECT SQL_CALC_FOUND_ROWS live.id, live.user, live.player,
SUBSTRING_INDEX(streams.channel, '_', -1) AS channel, bouquets.bouquet,
DATE_FORMAT(live.playtime, '%H:%i:%s %d/%m/%Y') AS playtime,
(SELECT CONCAT(
TIMESTAMPDIFF(day, l.playtime, NOW()) , 'd ',
MOD(TIMESTAMPDIFF(hour, l.playtime, NOW()), 24), 'h ',
MOD(TIMESTAMPDIFF(minute, l.playtime, NOW()), 60), 'm ',
MOD(TIMESTAMPDIFF(second, l.playtime, NOW()), 60), 's'
) FROM live l WHERE l.id = live.id) AS duration,
resellers.nick, live.ip, servers.server, live.id, live.id
FROM live LEFT JOIN streams ON live.stream=streams.id LEFT JOIN bouquets ON
live.bouquet=bouquets.id LEFT JOIN resellers ON live.reseller=resellers.id
LEFT JOIN servers ON live.server=servers.id
ORDER BY live.id desc LIMIT 0, 10;