0

私は現在、PHPBB3 フォーラムの何人かの個人が先週予約した合計時間のレポートを作成しようとしています。最初に、期待どおりに機能する次のクエリがありました。

SELECT forum_users.username, SUM(TIMESTAMPDIFF(SECOND, schedule_slots.time_starting, schedule_slots.time_finishing)) AS seconds 
FROM forum_users 
LEFT JOIN schedule_slots 
    ON forum_users.user_id = schedule_slots.user_id 
    AND schedule_slots.time_starting >= (CURDATE() - INTERVAL 1 WEEK) 
    AND schedule_slots.is_del = 0 
    AND schedule_slots.channel = 0
WHERE (forum_users.group_id = 8 OR forum_users.group_id = 5 OR forum_users.group_id = 14) 
GROUP BY forum_users.username 
ORDER BY upper(forum_users.username)

ただし、別のテーブルに参加すると、タイムスタンプの差が誤って計算されてしまいます (より高い)。これが私の新しい非動作ステートメントです。

SELECT forum_users.username, SUM(TIMESTAMPDIFF(SECOND, schedule_slots.time_starting, schedule_slots.time_finishing)) AS seconds, group_concat(DISTINCT forum_user_group.group_id) AS user_groups 
FROM forum_users 
LEFT JOIN schedule_slots 
    ON forum_users.user_id = schedule_slots.user_id 
    AND schedule_slots.time_starting >= (CURDATE() - INTERVAL 1 WEEK) 
    AND schedule_slots.is_del = 0 
    AND schedule_slots.channel = 0 
LEFT JOIN forum_user_group 
    ON forum_user_group.user_id = forum_users.user_id 
WHERE (forum_users.group_id = 8 OR forum_users.group_id = 5 OR forum_users.group_id = 14 OR forum_users.group_id = 12) 
GROUP BY forum_users.username 
ORDER BY upper(forum_users.username)

私はこれに空白を描いています、そしてあなたの助けは大歓迎です.

4

1 に答える 1

0

タイムスタンプの差は正しく計算されますが、ユーザーが属するグループの数を掛けます。

私は試してみます:

SELECT forum_users.username, SUM(TIMESTAMPDIFF(SECOND, schedule_slots.time_starting, schedule_slots.time_finishing)) AS seconds, 
(select group_concat(DISTINCT group_id) from forum_user_group WHERE forum_user_group.user_id = forum_users.user_id) AS user_groups 
FROM forum_users 
LEFT JOIN schedule_slots 
ON forum_users.user_id = schedule_slots.user_id 
AND schedule_slots.time_starting >= (CURDATE() - INTERVAL 1 WEEK) 
AND schedule_slots.is_del = 0 
AND schedule_slots.channel = 0 
WHERE (forum_users.group_id = 8 OR forum_users.group_id = 5 OR forum_users.group_id = 14 OR forum_users.group_id = 12) 
GROUP BY forum_users.username, user_groups 
ORDER BY upper(forum_users.username)
于 2013-04-10T15:20:26.773 に答える