select 年龄 ,儿子 from Table1 where 父亲=张三 union select 年龄 ,儿子 from Table2 where 父亲=张三个人 认为表设计的很不合理 一个表的 姓名 列 有中文 又有英文
select distinct table1.姓名,table1.年龄,table1.父亲 from table1,table2 where table1.姓名 like '%张儿%' or table1.姓名 in ( select table2.儿子 from table2 where table2.姓名 in ( select table1.父亲 from table1 where table1.姓名 like '%张儿%') ) ;
Table1中有以下数据
姓名 年龄 父亲
------------------
张儿1 10 张三
张儿2 12 张三
zhang儿 14
王儿 15 王五Table2中有以下数据姓名 年龄 儿子
------------------
张三 30 张儿1
张三 30 zhang儿
李四 40 李儿1
现在的要求是:1、table1中姓名 like 张儿的所有数据
2、在table1中找到张儿的父亲,再到table2中找到相同父亲的所有儿子
3、把两个结果集合返回例如:
输入"张儿",正确的返回是:张儿1 10 张三
张儿2 12 张三
zhang儿 14 张三
不知楼主第二个表是否存在一个儿子存在两个父亲?
union
select 年龄 ,儿子 from Table2 where 父亲=张三个人 认为表设计的很不合理 一个表的 姓名 列 有中文 又有英文
where table1.姓名 like '%张儿%'
or table1.姓名 in ( select table2.儿子 from table2
where table2.姓名 in ( select table1.父亲 from table1
where table1.姓名 like '%张儿%')
)
;