我在我正在构建的自定义论坛应用程序中使用了一个有点复杂的查询。我仍然不完全理解子查询,我不知道它们是否导致查询的其余部分出现问题。我正在尝试获取某个板块中的线程列表,其中包含线程的作者信息,它的最新帖子以及最新帖子的作者信息。该列表还应包含该线程的回复数。但是,回复计数不应包括该主题的第一条消息。
以下是我收到的完整错误消息:
Expression #10 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'commandcenter.lm.message_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
以下是完整编译的查询:
SELECT
`t`.`thread_id`, `t`.`board_id`, `t`.`author_id`, `t`.`subject`, `t`.`date_posted`,
`a`.`member_id` AS `author_id`, `a`.`display_name` as `author_display_name`,
`r`.`thread_id` as `r_thread_id`, COUNT(r.message_id) as total_replies,
`lm`.`message_id`, `lm`.`author_id` as `lm_author_id`, `lm`.`date_posted` as `lm_date_posted`
FROM `forum_threads` as `t`
LEFT JOIN `members` as `a` ON `t`.`author_id` = `a`.`member_id`
LEFT JOIN `forum_messages` as `lm` ON `lm`.`message_id` = (SELECT MAX(message_id) FROM forum_messages WHERE thread_id = t.thread_id)
LEFT JOIN `members` as `lma` ON `lm`.`author_id` = `lma`.`member_id`
LEFT JOIN `forum_messages` as `r` ON `t`.`thread_id` = `r`.`thread_id` AND `r`.`message_id` != (SELECT MIN(message_id) FROM forum_messages WHERE thread_id = t.thread_id)
GROUP BY `t`.`thread_id`
有人有什么想法吗?
注意:我正在使用CodeIgniter的查询构建器类,它不会向子查询添加反引号。