简体   繁体   English

MySQL#1241-操作数在计数时应包含1列

[英]MySQL #1241 - Operand should contain 1 column(s) on counting

I am running this query, and I am getting ** #1241 - Operand should contain 1 column(s)** error: 我正在运行此查询,并且得到**#1241-操作数应包含1列**错误:

  SELECT `forumCategories`.`id`, `forumCategories`.`name`, `forumCategories`.`order`, `forumCategories`.`description`, `forumCategories`.`date_created`, COUNT(forumPosts.forumCategory_id) as postCount,
    (SELECT `forumPosts`.*, `forumChildPosts`.`id`, `forumChildPosts`.`forumPost_id`, COUNT(forumChildPosts.forumPost_id) as childCount FROM `forumChildPosts` LEFT JOIN `forumPosts` ON `forumPosts`.`id` = `forumChildPosts`.`forumPost_id` GROUP BY `forumPosts`.`id`) AS childCount
    FROM `forumCategories` 
    LEFT JOIN `forumPosts` ON `forumCategories`.`id` = `forumPosts`.`forumCategory_id` 
    GROUP BY `forumCategories`.`id` 
    ORDER BY `forumCategories`.`order` DESC

I have 3 tables: 我有3张桌子:

forumCategories
forumPosts | forumPosts.forumCategory_id = forumCategories.id
forumChildPosts | forumChildPosts.forumPosts_id = forumPosts.id

I want to count all posts for the forum category, and them I want to count all the child posts that belongs to that forum category. 我要计算论坛类别的所有帖子,而我要计算属于该论坛类别的所有子帖子。 How can I do this? 我怎样才能做到这一点?

You can't select several items with a subselect and then give them one name. 您不能通过子选择来选择多个项目,然后给它们起一个名字。 Now you're getting everything from forumPosts, something from forumChildPosts etc and trying to put that into a single column, childCount. 现在,您可以从forumPosts中获取所有内容,从forumChildPosts中获取某些内容,然后尝试将其放入单个列childCount中。 This is not allowed. 这是不允许的。

It might be enough to remove all other result columns from that select and only leave the count ? 从该选择中删除所有其他结果列可能就足够了,仅保留count

I couldn't try it, is that makes sense ? 我无法尝试,这有意义吗? But you can't get nested results from mysql due to its limitation, MYSQL is a Matrix table. 但是由于其局限性,您无法从mysql获取嵌套结果,MYSQL是一个Matrix表。

SELECT `forumCategories`.`id`,
       `forumCategories`.`name`,
       `forumCategories`.`order`,
       `forumCategories`.`description`,
       `forumCategories`.`date_created`,
       COUNT(forumPosts.forumCategory_id) AS postCount,
      (SELECT COUNT(forumChildPosts.forumPost_id) AS childCount FROM `forumChildPosts` LEFT JOIN `forumPosts` ON `forumPosts`.`id` = `forumChildPosts`.`forumPost_id` GROUP BY `forumPosts`.`id`) AS childCount
FROM `forumCategories`
LEFT JOIN `forumPosts` ON `forumCategories`.`id` = `forumPosts`.`forumCategory_id`
GROUP BY `forumCategories`.`id`
ORDER BY `forumCategories`.`order` DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM