表一person
字段
id
sex
name
age
其中sex中男用“M”,女用“F”表示;
表二relation
字段
id1
id2
relation
其中relation用以下规则表示:
是子女关系,则为“0”,id1是父母,id2是子女;是夫妻关系,则“1”,id1是夫,id2是妻。
问1.有子女的母亲姓名
2.有夫妻关系的夫和妻的名字,如果没有妻子,则显示空格
3.夫妻年龄之和大于65岁的夫妻中丈夫的姓名
答案:
1.
SELECT person.name mother,person.id id
FROM person
WHERE person.id IN (select id1 from relation where relation = 0) AND person.sex = 'F';
2.
SELECT a.name 丈夫姓名,b.name 妻子姓名 FROM(SELECT * FROM PERSON a,PERSON b) tmpperson LEFT JOIN relation tmprelation ON a.id=tmprelation.id1 AND b.id=tmprelation.id2 AND tmprelation.relation=1;
3.
SELECT a.name 丈夫姓名,FROM((SELECT * FROM PERSON a,PERSON b) tmpperson LEFT JOIN relation tmprelation ON a.id=tmprelation.id1 AND b.id=tmprelation.id2 AND tmprelation.relation=1) tmp WHERE (a.age+b.age)>65;