我想从我们的mysql数据库中获取最热门的博客海报目前,它的一部分工作是:SELECT `username` , COUNT( `owner_id` ) AS Occurences, `owner_id`FROM `engine4_blog_blogs` , `engine4_users`WHERE `engine4_users`.`user_id` = `owner_id`GROUP BY `owner_id`ORDER BY Occurences DESCLIMIT 1产生:username / Occurences / owner_idjack91 / 10 / 4这是正确的。现在,我需要的是也让人的缩略图图标注册给他们,所以我尝试这样做:SELECT `username` , COUNT( `engine4_blog_blogs`.`owner_id` ) AS Occurences, `owner_id`, `storage_path`FROM `engine4_blog_blogs` , `engine4_users`, `engine4_storage_files`WHERE `engine4_users`.`user_id` = `engine4_blog_blogs`.`owner_id` AND `engine4_users`.`user_id` = `engine4_storage_files`.`user_id`GROUP BY `engine4_blog_blogs`.`owner_id`ORDER BY Occurences DESCLIMIT 1产生:username / Occurences / owner_id / storage_pathjack91 / 2480 / 4 / (public/user/1f/001f_250b.JPG)用户名、所有者id和存储路径是正确的,但由于发生此事件的值不正确,数据库的计数是多少?我想,通过指定COUNT( cc > cc > cc“cc”,它只会计算该字段——我还必须补充,列字段 >只存在于表中。现在我尝试了各种排列,包括连接、内部连接和左连接,并且都产生了相同的结果。。。不正确的count()。基本上我在寻找这个输出:username / Occurences / owner_id / storage_pathjack91 / 10 / 4 / (public/user/1f/001f_250b.JPG)有人知道我做错了什么吗(请记住,我已经10年没碰过sql了)?谢谢你的帮助! 最佳答案 使分组正确工作。必须按所有非聚合列分组:SELECT `username` , COUNT( DISTINCT `engine4_blog_blogs`.`blog_id` ) AS Occurences, `owner_id`, `storage_path`FROM `engine4_blog_blogs` , `engine4_users`, `engine4_storage_files`WHERE `engine4_users`.`user_id` = `engine4_blog_blogs`.`owner_id` AND `engine4_users`.`user_id` = `engine4_storage_files`.`user_id`GROUP BY 1,3,4ORDER BY Occurences DESCLIMIT 1您还需要在DISTINCT中添加COUNT()关键字,以获取不同作者的博客数量*Mysql支持非标准的分组语法-如果您坚持使用标准,它将如您所期望的那样工作。关于mysql - MySQL COUNT()函数不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29101425/
10-14 14:11