mysql求平均值的问题,急!

lonan 2009-11-19 01:02:48
采用PHP+MYSQL
表 company
userid name status
1 天才 1
2 英雄 1
3 鬼才 0

表 dp
gid userid sort1 sort2
1 1 4 3
2 2 2 3
3 2 2 1
4 2 3 2
5 2 3 2

列出 status=1 的情况下表company中的数据并计算表dp中userid值相同的sort1和sort2的平均值
name avgsort1 avgsort2
天才 4 3
英雄 2.5 2

请教高手用SQL语句怎样实现啊???
...全文
449 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqc851011 2010-08-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baiyuxiong 的回复:]
SQL code
SELECT company.name AS name, AVG( sort1 ) AS avgsort1, AVG( sort2 ) AS avgsort2
FROM company, dp
WHERE STATUS =1
GROUP BY dp.userid
[/Quote]
完全正确
hbbliyong 2010-03-19
  • 打赏
  • 举报
回复
非常感谢,解决了问题
t240034137 2009-11-19
  • 打赏
  • 举报
回复
怎么像回到MSSQL版了!
defaultError 2009-11-19
  • 打赏
  • 举报
回复
学习
乐游 之 神话 2009-11-19
  • 打赏
  • 举报
回复
学习
zmjsg 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baiyuxiong 的回复:]
SQL codeSELECT company.nameAS name,AVG( sort1 )AS avgsort1,AVG( sort2 )AS avgsort2FROM company, dpWHERE STATUS=1GROUPBY dp.userid
[/Quote]

顶。。。。
zmjsg 2009-11-19
  • 打赏
  • 举报
回复
学习
codeyell-com 2009-11-19
  • 打赏
  • 举报
回复
1楼强悍
codeyell-com 2009-11-19
  • 打赏
  • 举报
回复
SELECT company.name AS name, AVG( sort1 ) AS avgsort1, AVG( sort2 ) AS avgsort2
FROM company, dp
WHERE STATUS =1
GROUP BY dp.userid
阿_布 2009-11-19
  • 打赏
  • 举报
回复

SELECT c.*,AVG(d.sort1) avgsort1,AVG(d.sort2) avgsort2
FROM company c,dp d
WHERE c.userid=d.userid AND c.status=1
GROUP BY c.userid;
dzxccsu 2009-11-19
  • 打赏
  • 举报
回复

--测试数据
if Object_id('company') is not null
drop table company
create table company(
id bigint not null,
[name] varchar(50) not null,
state int not null
)
insert into company
select '1','天才','1'
union all
select '2','英雄','1'
union all
select '3','鬼才','0'

if Object_id('dp') is not null
drop table dp
create table dp(
gid bigint not null,
id bigint not null,
sort1 float not null,
sort2 float not null
)
insert into dp
select '1','1','4','3'
union all
select '2','2','2','3'
union all
select '3','2','2','1'
union all
select '4','2','3','2'
union all
select '5','2','3','2'
--查询
select a.[name],b.sort1,b.sort2 from ((select * from company where state='1')a left join (select id,avg(sort1*1.0) as sort1,avg(sort2*1.0) as sort2 from dp group by id)b on a.id=b.id)
--结果
天才 4.000000 3.000000
英雄 2.500000 2.000000

21,886

社区成员

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

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