Kilka zapytań napisanych przy okazji tworzenia konwertera phpbb by przemo -> ipb. Tabele bez prefiksów.
Synchronizacja/przeliczenie różnych wartości w prywatnych wiadomościach
UPDATE message_topics AS mt
SET
mt_replies =
(
SELECT COUNT(*) - 1
FROM message_posts AS mp
WHERE mp.msg_topic_id = mt.mt_id
),
mt_last_msg_id =
(
SELECT MAX(mp.msg_id)
FROM message_posts AS mp
WHERE mp.msg_topic_id = mt.mt_id
),
mt_first_msg_id =
(
SELECT MIN(mp.msg_id)
FROM message_posts AS mp
WHERE mp.msg_topic_id = mt.mt_id
),
mt_last_post_time =
(
SELECT MAX(mp.msg_date)
FROM message_posts AS mp
WHERE mp.msg_topic_id = mt.mt_id
),
mt_hasattach =
(
SELECT COUNT(*)
FROM attachments AS a
WHERE attach_rel_module ='msg'
AND attach_rel_id IN
(
SELECT msg_id
FROM message_posts AS mp
WHERE mp.msg_topic_id = mt.mt_id
)
);
UPDATE message_topic_user_map AS map
SET map_last_topic_reply =
(
SELECT MAX(mp.msg_date)
FROM message_posts AS mp
WHERE mp.msg_topic_id = map.map_topic_id
);
Przeliczenie komentarzy i ocen w galerii:
UPDATE gallery_images AS i
SET comments =
(
SELECT COUNT(*)
FROM gallery_comments AS c
WHERE i.id = c.img_id
),
lastcomment =
(
SELECT MAX(c.post_date)
FROM gallery_comments AS c
WHERE i.id = c.img_id
),
ratings_total =
(
SELECT SUM(rate)
FROM gallery_ratings AS r
WHERE r.img_id = i.id
),
ratings_count =
(
SELECT COUNT(*)
FROM gallery_ratings AS r
WHERE r.img_id = i.id
);