select a.* from A表 a where NA like '%高%' and exists( select 1 from A表 where NA=a.MA and GR<a.GR --不知道你这个学历高低的判断依据是什么? )
--建表 declare @a table(NA varchar(8),WN int,GR tinyint,MA int) insert into @a select '高一',1,1,2 union all select '李一',2,2,2 union all select '高二',3,3,2 union all select '高三',4,4,2 union all select '李二',5,3,2 union all select '李二',6,1,2--查询一 select x.* from @a x left join @a y on x.MA=y.WN where x.GR>y.GR and x.NA like '%高%' --查询二 select * from @a x where NA like '%高%' and GR>(select GR from @a y where x.MA=y.WN)--查询三 select * from @a a where NA like '%高%' and exists( select 1 from @a where WN=a.MA and GR<a.GR )--结果 /* NA WN GR MA -------- ----------- ---- ----------- 高二 3 3 2 高三 4 4 2 */
我是新手 相信您也有这种感觉 看试很简单 呵呵 但是实现起来 确实很费力(因为我们都不熟练) 看看 我这个 能不能给点线索 select * from a where wn in (select wn1 from ( select a.wn as wn1,a.gr as gr1,b.wn as wn2 ,b.gr as gr2 from a a, (select * from a where wn in (select ma from a) ) b where a.gr>b.gr and a.ma=b.wn and a.na like '%高%' group by a.wn,a.gr,b.wn,b.gr) c) 备注: 您的 条件是:1 员工名字里有“高” 2:员工所属经理的学历低于员工本人的学历 我认为是 AND 了 如果是OR 的话 也可以修改 第二 学历的比较 我当作数值型字段 (视您表的具体情况判断)呵呵 开心 快乐
from A表 a
where NA like '%高%' and exists(
select 1 from A表 where NA=a.MA
and GR<a.GR --不知道你这个学历高低的判断依据是什么?
)
declare @a table(NA varchar(8),WN int,GR tinyint,MA int)
insert into @a
select '高一',1,1,2 union all
select '李一',2,2,2 union all
select '高二',3,3,2 union all
select '高三',4,4,2 union all
select '李二',5,3,2 union all
select '李二',6,1,2--查询一
select x.* from @a x left join @a y
on x.MA=y.WN
where x.GR>y.GR and x.NA like '%高%' --查询二
select * from @a x
where NA like '%高%' and GR>(select GR from @a y where x.MA=y.WN)--查询三
select *
from @a a
where NA like '%高%' and exists(
select 1 from @a where WN=a.MA
and GR<a.GR
)--结果
/*
NA WN GR MA
-------- ----------- ---- -----------
高二 3 3 2
高三 4 4 2
*/
select * from a where wn in (select wn1 from (
select a.wn as wn1,a.gr as gr1,b.wn as wn2 ,b.gr as gr2 from a a,
(select * from a where wn in (select ma from a) ) b
where a.gr>b.gr and a.ma=b.wn and a.na like '%高%'
group by a.wn,a.gr,b.wn,b.gr) c)
备注: 您的 条件是:1 员工名字里有“高”
2:员工所属经理的学历低于员工本人的学历
我认为是 AND 了 如果是OR 的话 也可以修改
第二 学历的比较 我当作数值型字段 (视您表的具体情况判断)呵呵 开心 快乐