---
title: "sql查询点滴记录"
author: "dingchd"
date: "2014-01-16T11:25:05Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2014-01-16T11:25:05Z"
id: 2004854
url: "https://dingchd.iteye.com/blog/2004854"
---

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;

基本思想，内嵌视图