2010-05-18 115 views
16

正如標題所說,我想爲這一個解決辦法...MySQL的子查詢LIMIT

SELECT 
    comments.comment_id, 
    comments.content_id, 
    comments.user_id, 
    comments.`comment`, 
    comments.comment_time, 
    NULL 
FROM 
    comments 
WHERE 
    (comments.content_id IN (SELECT content.content_id FROM content WHERE content.user_id = 1 LIMIT 0, 10)) 

乾杯

回答

18
SELECT comments.comment_id, 
     comments.content_id, 
     comments.user_id, 
     comments.`comment`, 
     comments.comment_time, 
     NULL 
FROM (
     SELECT content.content_id 
     FROM content 
     WHERE content.user_id = 1 
     LIMIT 10 
     ) q 
JOIN comments 
ON  comments.content_id = q.content_id 

你可能會想一個ORDER BY添加到嵌套查詢。

+1

非常感謝這一個..那麼另一件事..有沒有辦法限制評論2每content_id? – 2010-05-18 10:46:38

+0

@ atif089:是的,看到這篇文章:http://explainextended.com/2009/03/06/advanced-row-sampling/ – Quassnoi 2010-05-18 10:57:39

+0

感謝您的鏈接..這不會太重?即時通訊使用這個定製的社交網站 – 2010-05-18 10:59:24