我需要執行mysql查詢以計算用戶郵箱使用的空間。 消息線程可能由兩方 (發件人/收件人)擁有多個郵件(回覆,跟進),並標記有一個或多個標記(收件箱,發件等)。從幾個表中獲取不同的結果
必須滿足以下條件:
a)用戶是消息的收件人或作者;
b)消息由任何標籤標記:1,2,3,4;
c)中不同的僅記錄,即如果線程,包含消息標記有 4分的標籤(例如圖1和4的多於一個:收件箱和已發送)的計算 是在一個標籤僅
我曾嘗試下面的查詢,但我沒能獲得不同的值 - 該 主題/值複製:
SELECT SUM(LENGTH(subject)+LENGTH(body)) AS sum
FROM om_msg_message omm
JOIN om_msg_index omi ON omm.mid = omi.mid
JOIN om_msg_tags_index omti ON omi.thread_id = omti.thread_id AND omti.uid = user_id
WHERE (omi.recipient = user_id OR omi.author = user_id) AND omti.tag_id IN (1,2,3,4)
GROUP BY omi.mid;
表的結構:
om_msg_message - 字段標題和正文都被計算
+--------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+------------------+------+-----+---------+----------------+ | mid | int(10) unsigned | NO | PRI | NULL | auto_increment | | subject | varchar(255) | NO | | NULL | | | body | longtext | NO | | NULL | | | timestamp | int(10) unsigned | NO | | NULL | | | reply_to_mid | int(10) unsigned | NO | | 0 | | +--------------+------------------+------+-----+---------+----------------+
om_msg_index
+-----+-----------+-----------+--------+--------+---------+ | mid | thread_id | recipient | author | is_new | deleted | +-----+-----------+-----------+--------+--------+---------+ | 1 | 1 | 1392 | 1211 | 0 | 0 | | 2 | 1 | 1211 | 1392 | 1 | 0 | +-----+-----------+-----------+--------+--------+---------+
om_msg_tags_index
+--------+------+-----------+ | tag_id | uid | thread_id | +--------+------+-----------+ | 1 | 1211 | 1 | | 4 | 1211 | 1 | | 1 | 1392 | 1 | | 4 | 1392 | 1 | +--------+------+-----------+
考慮提供適當的DDL和/或sqlfiddle連同SET – Strawberry