MySQL:选择tag = x的标签

时间:2013-01-09 03:43:22

标签: mysql select join

我有table media,tags,tag_content

c_ media

mid (primary)
m_title (varchar)
m_date (int 11, unix timestamp) index

代码

t_id (primary)
t_name (varchar)
t_type (varchar)

key: tag (columns t_name, t_type)

tag_content

tc_id (int 11) (same as t_id from tags table)
tc_cid (int 11) (same as mid from c_media table)
key: tc (columns tc_id, tc_cid)

目标是从故事中获得顶级标签,这些标签也有一个共同的标签(在本例中为162)并且是在过去24小时内创建的。这似乎有效,但很慢,谢谢你的帮助。

SELECT t_id, t_name, t_type, COUNT(t2.tc_id) AS total_tags FROM c_media
INNER JOIN tag_content t1 ON t1.tc_cid = mid AND t1.tc_id = '162'
INNER JOIN tag_content t2 ON t2.tc_cid = t1.tc_cid AND t2.tc_id != t1.tc_id
LEFT JOIN tags ON t_id = t2.tc_id
WHERE m_date > UNIX_TIMESTAMP( now( ) -86400 ) 
AND t_type NOT IN ('Topic', 'Position')
GROUP BY t2.tc_id
ORDER BY total_tags DESC
LIMIT 30 

0 个答案:

没有答案