求实现统计父表信息平均值

uczone 2009-12-06 11:04:53
我在做文章发表时有两个表【content】【writer】

【content】表结构和数据如下:
id title
------------------
1 aaaaaa
2 bbbbbb
3 cccccc
4 dddddd

【writer】表结构和数据如下:
arcid author
-------------------------
1 张三
1 李四
2 张三
3 王五
4 李四
4 王五

其中【writer】表中的【arcid】字段与【content】表的【id】相关联对应

我想要实现的目的是统计出每个作者共计投了多少篇文章,例:一篇文章有两个人投稿的话,每个人只占0.5篇,如果3个人投稿的话,每个人只占0.3篇,即只保留一位小数。

我现有的SQL语句得到的结果是:一篇文章,参与几位作者的话,就分别给他们记一篇。不知道能否实现平均分统计的要求呢?谢谢

现有sql语句如下:
Select b.author,count(a.id) as cc From `content` as a INNER JOIN `writer` as b on a.id=b.arcid group by b.author order by count(a.id) desc
...全文
82 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2009-12-06
  • 打赏
  • 举报
回复
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式https://forum.csdn.net/BList/OtherDatabase

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试。

uczone 2009-12-06
  • 打赏
  • 举报
回复
我完整的语句应该如下:
Select b.author,count(a.id) as cc From `content` as a INNER JOIN `writer` as b on a.id=b.arcid where a.typeid in (21,28,29,30,31,32) and a.islock=1 group by b.author order by count(a.id) desc
ACMAIN_CHM 2009-12-06
  • 打赏
  • 举报
回复
另外楼主下次请一次把问题讲清楚,不要回答完一次,又多出一个新的要求或限制。

建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式https://forum.csdn.net/BList/OtherDatabase

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试。

ACMAIN_CHM 2009-12-06
  • 打赏
  • 举报
回复
select a.author,ROUND(sum(1/b.cnt),1)
from writer a inner join (
select arcid,count(*) as cnt
from writer w inner join content c on w.arcid=c.id
where c.islock=1
) b on a.arcid=b.arcid
group by a.author
uczone 2009-12-06
  • 打赏
  • 举报
回复
谢谢您的回复,但是我还有疑问,我的content 有一个识别文章是否通过审核的字段【islock】当为1时通过审核,只统计通过审核的,语句又该如何写呢?谢谢
阿_布 2009-12-06
  • 打赏
  • 举报
回复

-- 保留小数点后一位

select w.author,sum(t.ct)
from writer w left join
(select arcid,format(1/count(*),1) ct
from writer
group by arcid) t
on w.arcid=t.arcid
group by w.author;

阿_布 2009-12-06
  • 打赏
  • 举报
回复

mysql> select * from writer;
+-------+--------+
| arcid | author |
+-------+--------+
| 1 | 张三 |
| 1 | 李四 |
| 2 | 张三 |
| 3 | 王五 |
| 4 | 李四 |
| 4 | 王五 |
+-------+--------+
6 rows in set (0.00 sec)

mysql> select w.author,sum(t.ct)
-> from writer w left join
-> (select arcid,1/count(*) ct
-> from writer
-> group by arcid) t
-> on w.arcid=t.arcid
-> group by w.author;
+--------+-----------+
| author | sum(t.ct) |
+--------+-----------+
| 张三 | 1.5000 |
| 李四 | 1.0000 |
| 王五 | 1.5000 |
+--------+-----------+
3 rows in set (0.02 sec)

阿_布 2009-12-06
  • 打赏
  • 举报
回复


select w.author,sum(t.ct) sumnum
from writer w left join
(select t1.arcid,format(1/count(*),1) ct
from writer t1,content t2
where t1.arcid=t2.id and t2.islock=1
group by arcid) t
on w.arcid=t.arcid
group by w.author
order by sumnum desc
limit 30;

uczone 2009-12-06
  • 打赏
  • 举报
回复
感谢zhoupuyue 问题已经解决
阿_布 2009-12-06
  • 打赏
  • 举报
回复

select w.author,sum(t.ct)
from writer w left join
(select t1.arcid,format(1/count(*),1) ct
from writer t1,content t2
where t1.arcid=t2.id and t2.islock=1
group by arcid) t
on w.arcid=t.arcid
group by w.author;
-- 正确结果应该是:

+--------+-----------+
| author | sum(t.ct) |
+--------+-----------+
| 张三 | 1.5 |
| 李四 | 1 |
| 王五 | 1.5 |
+--------+-----------+

uczone 2009-12-06
  • 打赏
  • 举报
回复
CREATE TABLE `cms`.`content` (
`id` INT( 6 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 50 ) NOT NULL ,
`islock` TINYINT( 1 ) NOT NULL
) ENGINE = InnoDB;


CREATE TABLE `cms`.`writer` (
`arcid` INT( 6 ) NOT NULL ,
`author` VARCHAR( 50 ) NOT NULL
) ENGINE = InnoDB;


INSERT INTO `cms`.`content` (
`id` ,
`title` ,
`islock`
)
VALUES (
NULL , 'aaaaaa', '1'
), (
NULL , 'bbbbbb', '1'
), (
NULL , 'cccccc', '1'
), (
NULL , 'dddddd', '1'
);


INSERT INTO `cms_content`.`writer` (
`arcid` ,
`author`
)
VALUES (
'1', '张三'
), (
'1', '李四'
), (
'2', '张三'
), (
'3', '王五'
), (
'4', '李四'
), (
'4', '王五'
);


我的查询语句如下:
SELECT b.author, count( a.id ) AS cc
FROM `content` AS a
INNER JOIN `writer` AS b ON a.id = b.arcid
WHERE a.islock =1
GROUP BY b.author
ORDER BY count( a.id ) DESC
LIMIT 0 , 30


得到结果如下:
author 	cc
张三 2
李四 2
王五 1


而我希望得到的统计结果,应为:
author 	cc
张三 1.5
李四 1.5
王五 1

即,精确统计到保留一位小数就行了如遇整除不尽的,不用四舍五入。
要按我的语句,反而文章多出来了 望高手指教,谢谢
阿_布 2009-12-06
  • 打赏
  • 举报
回复


select w.author,sum(t.ct)
from writer w left join
(select t1.arcid,format(1/count(*),1) ct
from writer t1,content t2
where t1.arcid=t2.id and t2.typid in(21,28,29,30,31,32) and t2.islock=1
group by arcid) t
on w.arcid=t.arcid
group by w.author;

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧