我希望在备注详细信息页面中显示总备注,总喜欢,总喜欢。以下是从中获取数据的三个表。
tbl_note_actions
noteaction_note_id
int(11)NOT NULL,
noteaction_user_id
int(11)NOT NULL,noteaction_type
tinyint(1)NOT NULL COMMENT'1-> like,0-> dislike',
PRIMARY KEY(noteaction_note_id
,noteaction_user_id
)tbl_notes_comments
notepost_id
int(11)NOT NULL AUTO_INCREMENT,
notepost_note_id
int(11)NOT NULL,notepost_user_id
int(11)NOT NULL,
notepost_message
文本NOT NULL,
notepost_rating
tinyint(1)NOT NULL,
notepost_parent_id
int(11)NOT NULL,
notepost_added_on
日期时间NOT NULL,
notepost_active
tinyint(1)NOT NULL DEFAULT'1',
notepost_deleted
tinyint(1)NOT NULL DEFAULT'0',
PRIMARY KEY(notepost_id
)tbl_notes
note_id
int(11)NOT NULL AUTO_INCREMENT,
note_notetype_id
int(11)NOT NULL,
note_user_id
int(11)NOT NULL,
note_description
文本NOT NULL, PRIMARY KEY(note_id
)
它显示了总喜欢,完全不喜欢和总评论的错误计数。
这是我的问题:
SELECT
note_id,
note_notetype_id,
note_user_id,
note_name,
note_description,
COUNT( DISTINCT notepost_note_id) AS totComments,
note_id,
count(if(noteaction_type = 1, noteaction_note_id, NULL)) AS totlikes,
count( if(noteaction_type = 0, noteaction_note_id, NULL)) AS totdislikes
FROM `tbl_notes`
LEFT OUTER JOIN `tbl_users`
ON user_id= note_user_id
INNER JOIN `tbl_courses`
ON course_id = note_course_id
INNER JOIN `tbl_universities`
ON university_id = note_university_id
INNER JOIN `tbl_note_types`
ON notetype_id = note_notetype_id
LEFT OUTER JOIN `tbl_note_actions`
ON noteaction_note_id = note_id
LEFT OUTER JOIN `tbl_notes_posts`
ON notepost_note_id = note_id
WHERE `note_id` = '4'
GROUP BY note_id, notepost_id
结果喜欢1,不喜欢1和评论1但实际上它应该是1,不喜欢1和评论4。
请帮我解决一下
由于
答案 0 :(得分:0)
亲爱的,我想知道为什么你使用DISTINCT COUNT(DISTINCT notepost_note_id)AS totComments ,因为如果我有一个帖子有多个评论然后你使用DISTINCT它将返回1值总是为那个发布
删除DISTINCT并检查
答案 1 :(得分:0)
试试这可能会对你有所帮助
SELECT
note_id,
note_notetype_id,
note_user_id,
note_name,
note_description,
noteCount.totalComment AS totComments,
note_id,
count(if(noteaction_type = 1, noteaction_note_id, NULL)) AS totlikes,
count( if(noteaction_type = 0, noteaction_note_id, NULL)) AS totdislikes
FROM `tbl_notes`
LEFT JOIN (
SELECT note_id,count(notepost_note_id) as totalComment
FROM tbl_notes
GROUP BY note_id
) AS noteCount ON noteCount.note_id=tbl_notes.note_id