sql查询点滴记录

Q:查看所有低于部门平均工资的所有员工
表结构:
CREATE TABLE 'pp'(
'id' tinyint(4) default NULL,
'num' int(11) default NULL,
'name' varchar(255) default NULL,
'bm' varchar(255) default NULL
} ENGINE=InnoDB DEFAULT CHARSET=utf8;

A:sql语句如下
select pp.name,pp.num,pp.bm from pp,(select avg(num) as k,bm from pp group by bm) as b where pp.bm=b.bm and pp.num<b.k;

基本思想,内嵌视图

猜你喜欢

转载自dingchd.iteye.com/blog/2004854