2014-10-26 62 views
0

我寫了一個查詢SQL用於獲取與再評論最後意見:爲什麼操作員LIMIT不能正常工作?

SELECT c.*, ar.ArticleName, ar.idArticle, du.DetailToUsersName, du.DetailToUsersPhoto, COUNT(c.idCommentToArticle) AS CNT, CASE WHEN d.Count IS NULL THEN 0 ELSE d.Count END AS CountLikes 
FROM commenttoarticle c 

INNER JOIN (SELECT CommentToArticlePID FROM commenttoarticle 
GROUP BY CommentToArticlePID 
ORDER BY CommentToArticlePID LIMIT 3) AS articleComments 
USING (CommentToArticlePID) 

LEFT JOIN article ar ON c.CommentToArticleIdArticle = ar.idArticle 
LEFT JOIN detailtousers du ON du.idDetailToUsers = c.CommentToArticleIdUser 
LEFT JOIN `likes` d ON (d.IdNote = c.idCommentToArticle AND d.LikeType = 6) 

WHERE c.CommentToArticleIdArticle = 11 
GROUP BY c.idCommentToArticle 

那麼,爲什麼運營商LIMIT 3子查詢中選擇不工作?現在,這個查詢顯示,從表中的所有行commenttoarticle

我似乎需要做的somethink像爲:

SELECT... 
FROM (select * from commenttoarticle commenttoarticle c INNER JOIN 
      (SELECT distinct(CommentToArticlePID) 
       FROM commenttoarticle b 
       ORDER BY CommentToArticlePID 
        LIMIT 2) AS commenttoarticle USING (CommentToArticlePID)) as c 

      LEFT JOIN article ar ON c.CommentToArticleIdArticle = ar.idArticle... 

轉儲表commenttoarticle:

CREATE TABLE IF NOT EXISTS `commenttoarticle` (
    `idCommentToArticle` int(11) NOT NULL AUTO_INCREMENT, 
    `CommentToArticleTime` int(11) NOT NULL, 
    `CommentToArticleIdArticle` int(11) NOT NULL, 
    `CommentToArticleComment` text NOT NULL, 
    `CommentToArticleIdUser` int(11) NOT NULL, 
    `CommentToArticlePID` int(11) NOT NULL, 
    PRIMARY KEY (`idCommentToArticle`), 
    UNIQUE KEY `idCommentToArticle_UNIQUE` (`idCommentToArticle`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=59 ; 


INSERT INTO `commenttoarticle` (`idCommentToArticle`, `CommentToArticleTime`, `CommentToArticleIdArticle`, `CommentToArticleComment`, `CommentToArticleIdUser`, `CommentToArticlePID`) VALUES 
(29, 0, 11, 'продажам?\nИнтересует не мега-звезда, а именно предметный, руками умеющий продавать сам и помогающий выстраивать это бизнесам.', 459, 0), 
(30, 0, 11, '2', 459, 0), 
(31, 0, 11, '3', 459, 0), 
(36, 0, 11, '3.1', 459, 31), 
(37, 1413822798, 11, 'also facing that prob. on the plteform of win 7', 459, 29), 
(38, 0, 11, ' here i dont have internet connection.. @Samint Sinha thanks ill check it out maybe tomorrow.', 459, 29), 
(39, 0, 11, ' Select max id and you will have dhe last row returned', 459, 29), 
(32, 0, 11, '4', 459, 0), 
(44, 1414354324, 11, 'How to do', 456, 29), 
(45, 1414354469, 11, 'sfsfsf', 456, 29), 
(46, 1414354708, 11, 'dddd', 456, 29), 
(47, 1414357761, 11, 'sfsfs', 456, 0), 
(57, 1414370833, 39, 'kkkppppppp', 456, 0), 
(49, 1414358233, 11, 'VSF\nSFSF', 456, 0), 
(50, 1414359589, 11, 'How to do', 456, 0), 
(51, 1414359660, 11, 'sfsfsdf', 456, 0), 
(52, 1414361057, 11, 'SDFSF', 456, 0), 
(53, 1414364023, 11, 'dsfdsjfsifmsi', 456, 0), 
(54, 1414364031, 11, 'sdfdskjfnskf', 456, 52), 
(55, 1414364034, 11, 'sdfdskjfnskf', 456, 52), 
(56, 1414364044, 11, 'fndsdfnsofosfi', 456, 52), 
(58, 1414370841, 39, 'dfgdfgdgdgdgdgdfgdgdfg', 456, 0); 

轉儲表的文章:

CREATE TABLE IF NOT EXISTS `article` (
    `idArticle` int(11) NOT NULL AUTO_INCREMENT, 
    `ArticleName` varchar(255) NOT NULL, 
    `ArticleTime` int(11) NOT NULL, 
    `ArticleDescription` varchar(500) NOT NULL, 
    `ArticleText` text NOT NULL, 
    `ArticleToUserID` int(11) DEFAULT NULL, 
    `ArticleCategory` int(11) NOT NULL, 
    `ArticleView` int(11) NOT NULL, 
    `ArticleCountry` int(11) NOT NULL, 
    `ArticlePhoto` varchar(150) NOT NULL, 
    `ArticleCity` int(11) NOT NULL, 
    PRIMARY KEY (`idArticle`), 
    UNIQUE KEY `idArticle_UNIQUE` (`idArticle`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=50 ; 
+0

是什麼'CommentToArticlePID'持有?現在您正在對該列進行分組和限制,但不會在其他任何地方使用它。 – Gervs 2014-10-26 17:20:43

+0

也許我的查詢是錯誤的,所以可以在第二查詢中刪除GROUP BY'CommentToArticlePID',而不是解決方案 – AllenDegrud 2014-10-26 17:33:49

+0

不,我們不能刪除'GROUP BY CommentToArticlePID',因爲那樣的話,我們不能讓孩子們排 – AllenDegrud 2014-10-26 17:36:37

回答

1

通過構建自連接上的評論yuo're沒有限制,但繁殖的結果。 你需要的是在意見表

SELECT c.*, ar.ArticleName, 
    ar.idArticle, du.DetailToUsersName, 
    du.DetailToUsersPhoto, COALECSE(SUM(d.count), 0) 
FROM 
    (SELECT * 
    FROM 
     commenttoarticle 
    WHERE 
     CommentToArticleIdArticle = 11 
    ORDER BY 
     CommentToArticlePID, idCommentToArticle DESC 
    LIMIT 3 
    ) c 
LEFT JOIN 
    article ar 
    ON c.CommentToArticleIdArticle = ar.idArticle 
LEFT JOIN 
    detailtousers du 
    ON du.idDetailToUsers = c.CommentToArticleIdUser 
LEFT JOIN 
    likes d 
    ON (d.IdNote = c.idCommentToArticle AND d.LikeType = 6) 
GROUP BY 
    c.idCommentToArticle 

我所做的假設,即評論可以在喜歡錶中多個條目的單一子查詢,否則子查詢和不necesarry組。

1
The USING(column_list) clause names a list of columns that must exist in both tables. 

參考:http://dev.mysql.com/doc/refman/5.0/en/join.html

CommentToArticlePID是不是在你的第一個表。

此外,什麼是加入一個單列的點?是的,如果你想限制,這可能適用,但爲什麼我們LIMIT?

+0

我需要使用'LIMIT'來顯示所有子行的最後一行('PID')。該聯合只需要父母的評論。如果可以的話,請解釋你的答案,我不確定'CommentToArticlePID' – AllenDegrud 2014-10-26 16:54:29

+0

@AllenDegrud。不確定你在做什麼。我喜歡用'ON'而不是'USING',所以我會這樣描述它。您正在articleComments.CommentToArticlePID = commenttoarticle.CommentToArticlePID上將派生表'articleComments'連接到'commenttoarticle'。兩個表中是否存在CommentToArticl。另外,您在CommentToArticlePID上進行分組,因此您只能得到一個結果,因此LIMIT不會執行任何操作。 – user1032531 2014-10-26 17:41:13

+0

嗯,你可以用'ON'而不是'USING'來修改我的查詢嗎?可能我會理解我的錯誤? – AllenDegrud 2014-10-26 17:44:41