SQL查询系列1---

1.查询关系为夫妻,年龄相加大于60的信息

表1 信息表 info
id 编号  
sex 性别 0-女,1-男
age 年龄  
表2 关系表 rel
id1 编号1 外键
id2 编号2 外键
rel 关系 0-父女,1-夫妻

方法一

select * from (select * from rel where rel=1) r,info i1,info i2 where r.id1=i1.id and r.id2=i2.id and i1.age+i2.age>60;

  

方法二

select * from (select * from rel where rel=1) r join info i1 on (i1.id=r.id1) join info i2 on (i2.id = r.id2) where i2.age+i1.age>60;

  

原文地址:https://www.cnblogs.com/next-open/p/3690805.html