22,182
社区成员




select a.职工姓名, a.工资 ,经理=b.职工姓名,经理工资=b.工资
from tb2 a join tb2 b
on a.经理号 =b.职工号 and a.工资> b.工资
/*
职工姓名 工资 经理 经理工资
------------ ----------- -------- -----------
h 300 d 297
(1 行受影响)
*/
SELECT a.*,b.职位,b.职工姓名,b.工资
from 雇员表 a
left join 雇员表 b ON a. 部门号= b. 部门号 AND b.经理号 ='7839
where a.工资 >=b.工资
SELECT a.*,b.职位,b.职工姓名,b.工资
from 雇员表 a
left join 雇员表 b ON a. 部门号= b. 部门号 AND b.职位 ='部门经理'
where a.工资 >=b.工资
if object_id('tb') is not null
drop table tb
go
create table tb(职工号 varchar(10),职工姓名 varchar(10),职位 varchar(10),经理号 varchar(10),工资 int,部门号 varchar(10))
go
insert into tb
select '7369' , 'a' , '办事员' , '7902' ,80 , '20' union all
select
'7499' , 'b' , '办事员' , '7698' ,160 ,'30' union all
select
'7521', 'c' , '办事员' , '7698' , 125 , '30' union all
select
'7566' , 'd' , '部门经理', '7839' , 297 ,'20' union all
select
'7654' , 'e' , '办事员' , '7698' , 125 , '30' union all
select
'7698' , 'f' , '部门经理', '7839' , 285 ,'30' union all
select
'7782' , 'g' , '部门经理' ,'7839' , 245 , '10' union all
select
'7788', 'h' , '办事员' , '7566' ,300, '20' union all
select
'7839' , 'i' , '总经理' , '' , 500 , '10' union all
select
'7844' , 'j' , '办事员', '7698', 150 , '30'
go
select a.职工姓名,a.工资,b.职工姓名 as 经理姓名,b.工资 as 经理工资 from tb a left join tb b on a.经理号=b.职工号 where a.工资>b.工资
select * from tb
go
select * from tb where 工资>
(
select max(工资) from tb where 职位='部门经理'
)
--> (让你望见影子的墙)生成测试数据,时间:2009-04-01
if not object_id('tb2') is null
drop table tb2
Go
Create table tb2([职工号] Datetime,[职工姓名] nvarchar(1),[职位] nvarchar(4),[经理号] int,[工资] int,[部门号] int)
Insert tb2
select '7369',N'a',N'办事员',7902,80,20 union all
select '7499',N'b',N'办事员',7698,160,30 union all
select '7521',N'c',N'办事员',7698,125,30 union all
select '7566',N'd',N'部门经理',7839,297,20 union all
select '7654',N'e',N'办事员',7698,125,30 union all
select '7698',N'f',N'部门经理',7839,285,30 union all
select '7782',N'g',N'部门经理',7839,245,10 union all
select '7788',N'h',N'办事员',7566,300,20 union all
select '7839',N'i',N'总经理',500,10,null union all
select '7844',N'j',N'办事员',7698,150,30
Go
Select * from tb2
大于所有部门经理的
select * from tb2
where 职位<>'部门经理' and 工资> all(select 工资 from tb2 where 职位='部门经理' )
职工号 职工姓名 职位 经理号 工资 部门号
7788-01-01 00:00:00.000 h 办事员 7566 300 20
大于其中一个就可以的
select * from tb2
where 职位<>'部门经理' and 工资> some(select 工资 from tb2 where 职位='部门经理' )
if object_id('tb') is not null
drop table tb
go
create table tb(职工号 varchar(10),职工姓名 varchar(10),职位 varchar(10),经理号 varchar(10),工资 int,部门号 varchar(10))
go
insert into tb
select '7369' , 'a' , '办事员' , '7902' ,80 , '20' union all
select
'7499' , 'b' , '办事员' , '7698' ,160 ,'30' union all
select
'7521', 'c' , '办事员' , '7698' , 125 , '30' union all
select
'7566' , 'd' , '部门经理', '7839' , 297 ,'20' union all
select
'7654' , 'e' , '办事员' , '7698' , 125 , '30' union all
select
'7698' , 'f' , '部门经理', '7839' , 285 ,'30' union all
select
'7782' , 'g' , '部门经理' ,'7839' , 245 , '10' union all
select
'7788', 'h' , '办事员' , '7566' ,300, '20' union all
select
'7839' , 'i' , '总经理' , '' , 500 , '10' union all
select
'7844' , 'j' , '办事员', '7698', 150 , '30'
go
select a.*,b.职工姓名 as 经理姓名 from
(select a.*,部门经理工资=(select max(工资) from tb where 职工号=a.经理号) from tb a)a
left join tb b on a.经理号=b.职工号
where a.工资>a.部门经理工资
drop table tb