求一条SQL语句
两个表结构及内容如下:
table1(check_item)
t1_id name t3_id
1 名称1 1
2 名称2 2
3 名称3 2
4 名称4 2
5 名称5 3
table2(check_result)
t2_id t1_id t4_id content
1 2 10 内容1
2 3 10 内容2
3 4 10 内容3
4 5 20 内容4
第一步,使用以下语句查找table1中t3_id = 2的所有记录
select t1_id,name from table1 where t3_id = 2
则结果可能为(假定为视图view1)
t1_id name
2 名称2
3 名称3
4 名称4
第二步,在table2中依次查找满足view1中t1_id 分别为2,3,4以及t4_id = 10的content列即
select content from view1,table2 where view1.t1_id = table2.t1_id and t4_id = 10
最后要求的结果形式为
name content
名称2 内容1
名称3 内容2
名称4 内容3
其中要求name列必须存在,即即使没有满足条件的记录,也应该是如下的形式
最后要求的结果形式为
name content
名称2 null
名称3 null
名称4 null
请问如何用一条SQL语句完成如上查询
这个问题第1个回答:
SQL code
select
a.name,
b.content
from table1 a
left join table2 b
on a.t1_id=b.t1_id and t4_id=10
where a.t3_id=2
这个问题第2个回答:
SQL code
select t1.Name,t2.content from table1 t1, table2 t2 where t1.t1_id=t2.t2_id and t1.t3_id = 2
这个问题第3个回答:
更正
SQL code
select t1.Name,t2.content from table1 t1 left join table2 t2 where t1.t1_id=t2.t2_id and t1.t3_id = 2 and t2.t4_id=10
这个问题第4个回答:
非常感谢,结贴