表1(Table1)如下:
--------------------
Name NameId
-----------------------
张三 1
李四 2
王五 3表2(Table2)如下:
--------------------
NameId abc
-------------
2 80如何关联查询得出以下记录:
Name NameId abc
----------------------
张三 1 无成绩
李四 2 80
王五 3 无成绩
谢谢各位。
--------------------
Name NameId
-----------------------
张三 1
李四 2
王五 3表2(Table2)如下:
--------------------
NameId abc
-------------
2 80如何关联查询得出以下记录:
Name NameId abc
----------------------
张三 1 无成绩
李四 2 80
王五 3 无成绩
谢谢各位。
from table1 a left join table2 b on a.nameid = b.nameid
select a.*,isnull(b.abc,N'无成绩')
from T1 a left join T2 b
on a.name=b.name
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON T1.NAMEID=T2.NAMEID
select a.*,isnull(b.abc,N'无成绩') from T1 a,T2 b
表2(Table2)如下:
--------------------
NameId abc tt
-------------
2 80 1
1 60 1
1 70 2当我查询第1学期(tt=1)时,得出:
Name NameId abc tt
----------------------
张三 1 60 1
李四 2 80 1
王五 3 无成绩 1查询第2学期(tt=2)时,得出
Name NameId abc tt
----------------------
张三 1 70 2
李四 2 无成绩 2
王五 3 无成绩 2应该如何实现?
--create table select * from table1
Name NameId
---- -----------
张三 1
李四 2
王五 3(3 行受影响)select * from table2
NameId abc tt
----------- ----------- -----------
2 80 1
1 60 1
1 70 2(3 行受影响)
--查询第1学期(tt=1)时,得出:DECLARE @a int = 1 ----查询第1学期(tt=1)
select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
from table1 a
left join table2 b on a.NameId=b.NameId where b.tt = @a
Union all
select Name,NameId,'无成绩' abc,@a
from table1 a
where not exists (select * from table2 where a.NameId = NameId and tt = @a )--result:
Name NameId abc tt
---- ----------- ---------- -----------
张三 1 60 1
李四 2 80 1
王五 3 无成绩 1(3 行受影响)
--查询第2学期(tt=2)时,得出
DECLARE @a int = 2 ----查询第1学期(tt=2)
select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
from table1 a
left join table2 b on a.NameId=b.NameId where b.tt = @a
Union all
select Name,NameId,'无成绩' abc,@a
from table1 a
where not exists (select * from table2 where a.NameId = NameId and tt = @a )--result:
Name NameId abc tt
---- ----------- ---------- -----------
张三 1 70 2
李四 2 无成绩 2
王五 3 无成绩 2(3 行受影响)
--查询第1学期(tt=1)时,得出:DECLARE @a int = 1 ----查询第1学期(tt=1)
select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
from table1 a
join table2 b on a.NameId=b.NameId where b.tt = @a
Union all
select Name,NameId,'无成绩' abc,@a
from table1 a
where not exists (select * from table2 where a.NameId = NameId and tt = @a )--result:
Name NameId abc tt
---- ----------- ---------- -----------
张三 1 60 1
李四 2 80 1
王五 3 无成绩 1(3 行受影响)
--查询第2学期(tt=2)时,得出
DECLARE @a int = 2 ----查询第1学期(tt=2)
select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
from table1 a
join table2 b on a.NameId=b.NameId where b.tt = @a
Union all
select Name,NameId,'无成绩' abc,@a
from table1 a
where not exists (select * from table2 where a.NameId = NameId and tt = @a )--result:
Name NameId abc tt
---- ----------- ---------- -----------
张三 1 70 2
李四 2 无成绩 2
王五 3 无成绩 2(3 行受影响)
-------------------------------------------
DECLARE @a int = 2 ----查询第1学期(tt=2)
select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
from table1 a
join table2 b on a.NameId=b.NameId where b.tt = @a
Union all
select Name,NameId,'无成绩' abc,@a
from table1 a
where not exists (select * from table2 where a.NameId = NameId and tt = @a )
select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
from table1 a
join table2 b on a.NameId=b.NameId where b.tt = @a
Union all
select Name,NameId,'无成绩' abc,@a
from table1 a
where not exists (select * from table2 where a.NameId = NameId and tt = @a )
select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
from table1 a
join table2 b on a.NameId=b.NameId where b.tt = @a
Union all
select Name,NameId,'无成绩' abc,@a
from table1 a
where not exists (select * from table2 where a.NameId = NameId and tt = @a )