有A、B两个表,其中A表是人员信息表,B表是人员关系表
下面是这两个表的结构:A表
----------------------------
id |人员编号 number(10)
name |姓名 varchar(30)
age |年龄 number(3)
sex |性别 number(1)
----------------------------B表
----------------------------
id1 |人员编号1 number(10)
id2 |人员编号2 number(10)
relaction |关系 number(1)
----------------------------说明:relaction有两个值:
0表示id1与id2是夫妻关系,这时id1是丈夫的人员编号
id2是妻子的编号
1表示id1与id2是父子关系,这时id1是父亲的人员编号
id2是妻子的编号问:用一个SQL语句列出夫妻年龄之和大于50岁的丈夫的信息?请各位谈谈SQL的原理,各抒己见!谢谢!
下面是这两个表的结构:A表
----------------------------
id |人员编号 number(10)
name |姓名 varchar(30)
age |年龄 number(3)
sex |性别 number(1)
----------------------------B表
----------------------------
id1 |人员编号1 number(10)
id2 |人员编号2 number(10)
relaction |关系 number(1)
----------------------------说明:relaction有两个值:
0表示id1与id2是夫妻关系,这时id1是丈夫的人员编号
id2是妻子的编号
1表示id1与id2是父子关系,这时id1是父亲的人员编号
id2是妻子的编号问:用一个SQL语句列出夫妻年龄之和大于50岁的丈夫的信息?请各位谈谈SQL的原理,各抒己见!谢谢!
id2是妻子的编号”这句话怎么理解呢?应该说“1表示id1与id2是父女关系”
^^
where id in (select id1 from (select y.id1 id1,x.age age1,y.id2 id2,z.age age2,x.age+z.age total from a x,b y,a z
where x.id=y.id1 and z.id=y.id2 and y.relation='0'
)
where total>50)
where exists (select * from b x,a y,a z
where x.id1 = i.id1
and x.id1 = y.id
and x.id2 = z.id
and z.age + y.age > 50
and b.relation = 0)
(select (select age from a where id=id1) age1,(select age from a where id=id2) age2 from b where relaction=0) kk
where a.id in(select id1 from b)
and (kk.age1+kk.age2)>50
select * from
a,
(select (select age from a where id=id1) age1,(select age from a where id=id2) age2,id1 from b where relaction=0) kk
where a.id in(select id1 from b)
and (kk.age1+kk.age2)>50
and kk.id1=a.id
from a,b
where b.relaction = 0
and b.id1 + b.id2 > 50
and a.id = b.id1
id2是‘妻子’的编号”这里的妻子应该改成‘孩子’!
http://expert.csdn.net/Expert/topic/2129/2129026.xml?temp=7.221621E-02