2012-05-11 53 views
0

我有兩個表。拇指向上/向下Mysql查詢

my_comment_thumbs [thumb_member_id, Cmt_id, thumb] 
my_Comment [id, page, member_id, comment, ip] 

對於豎起大拇指,thumb='1',和拇指向下,thumb='0'

[不知道這是很清楚的。]

我一直試圖讓豎起大拇指和拇指向下計數爲每個評論。

到目前爲止,我已經得到了什麼[沒有什麼,我有充分的認識...]

SELECT 
SUM(IF(thumb='1', 1, 0)) AS t_up, 
SUM(IF(thumb='0', 1, 0)) AS t_down, 
id, member_id, comment, ip 
FROM my_Comment LEFT OUTER JOIN my_comment_thumbs ON member_id=thumb_member_id 
WHERE page='333' 

任何幫助是極大的讚賞。

回答

0

由於我沒有數據的設置,我可以測試了這一點,我認爲這會工作 - 但它沒有進行測試:

SELECT 
    (SELECT count(thumb) FROM my_comment_thumbs WHERE thumb=1) as t_up, 
    (SELECT count(thumb) FROM my_comment_thumbs WHERE thumb=0) as t_down, 
    id, member_id, comment, ip 
FROM my_Comment 
LEFT OUTER JOIN my_comment_thumbs ON member_id=thumb_member_id 
WHERE page='333' 

希望有所幫助。

+0

連接沒有必要。否則你讓我走向正確的方向。非常感謝你! – DaedBaet

0

for thumb = 1 or thumb = 0;

"SELECT COUNT(*) FROM my_comment_thumbs,my_Comment 
    WHERE 
     my_Comment.id=my_comment_thumbs.Cmt_id 
      AND 
     my_comment_thumbs.thumb=1 
      AND 
     member_id=thumb_member_id;" 
+0

謝謝,perdickss。我試圖讓拇指數[上下]加上評論數據。 – DaedBaet

+0

作爲t_up(SELECT count(*)FROM my_comment_thumbs WHERE thumb = my_Comment.id = my_comment_thumbs.Cmt_id AND my_comment_thumbs.thumb = 1 AND member_id = thumb_member_id AND page ='333'; –