有2张表
A表
id aname
1 一杆球
2 二杆球
3 三杆球
4 四杆球
5 五杆球
6 六杆球B 表
id aid bid bname
1 1 123 金牌
2 1 12 金牌
3 2 123 金牌
4 2 13 金牌
5 3 123 铜牌
6 4 123 金牌
7 5 11 金牌
8 6 22 银牌
9 3 33 金牌
10 4 111 金牌
11 4 333 铜牌显示的结果集:
id aname bid bname
1 一杆球 123 金牌
2 二杆球 123 金牌
3 三杆球 123 铜牌
4 四杆球 123 金牌
5 五杆球 123 null
6 六杆球 123 null以A表为基础查询bid编号为123的结果集,要求aname不重复。 小弟刚学编程,还请各位赐教!!不甚感激!!
A表
id aname
1 一杆球
2 二杆球
3 三杆球
4 四杆球
5 五杆球
6 六杆球B 表
id aid bid bname
1 1 123 金牌
2 1 12 金牌
3 2 123 金牌
4 2 13 金牌
5 3 123 铜牌
6 4 123 金牌
7 5 11 金牌
8 6 22 银牌
9 3 33 金牌
10 4 111 金牌
11 4 333 铜牌显示的结果集:
id aname bid bname
1 一杆球 123 金牌
2 二杆球 123 金牌
3 三杆球 123 铜牌
4 四杆球 123 金牌
5 五杆球 123 null
6 六杆球 123 null以A表为基础查询bid编号为123的结果集,要求aname不重复。 小弟刚学编程,还请各位赐教!!不甚感激!!
from a
left join b
on a.id=b.aid
from ta a
left join tb b on a.id = b.aid
where not exists(select 1 from tb where b.aid = aid qnd id < b.id)
LEFT JOIN B ON A.ID=B.AID AND B.BID='123'
from ta a
left join tb b on a.id = b.aid
and not exists(select 1 from tb where b.aid = aid qnd id < b.id)
from ta a
left join tb b on a.id = b.aid
where not exists(select 1 from tb where b.aid = aid qnd id < b.id)
and b.bid = 123
from a
left join b
on a.id=b.aid
And b.bid=123
from ta a
left join (select * from tb where bid = 123) b
on a.id = b.aid
Drop table [a]
Go
Create table [a]([id] int,[aname] nvarchar(3))
Insert a
Select 1,'一杆球' union all
Select 2,'二杆球' union all
Select 3,'三杆球' union all
Select 4,'四杆球' union all
Select 5,'五杆球' union all
Select 6,'六杆球'
Go
--Select * from a--> 生成测试数据表:bIf not object_id('[b]') is null
Drop table [b]
Go
Create table [b]([id] int,[aid] int,[bid] int,[bname] nvarchar(2))
Insert b
Select 1,1,123,'金牌' union all
Select 2,1,12,'金牌' union all
Select 3,2,123,'金牌' union all
Select 4,2,13,'金牌' union all
Select 5,3,123,'铜牌' union all
Select 6,4,123,'金牌' union all
Select 7,5,11,'金牌' union all
Select 8,6,22,'银牌' union all
Select 9,3,33,'金牌' union all
Select 10,4,111,'金牌' union all
Select 11,4,333,'铜牌'
Go
--Select * from b-->SQL查询如下:
select a.*,b.bid,b.bname
from a
left join (select * from b t where bid=(select max(bid) from b where aid=t.aid)) b
on a.id=b.aid
/*
id aname bid bname
----------- ----- ----------- -----
1 一杆球 123 金牌
2 二杆球 123 金牌
3 三杆球 123 铜牌
4 四杆球 333 铜牌
5 五杆球 11 金牌
6 六杆球 22 银牌(6 行受影响)
*/应该是这个结果吧
select a.id,a.name,b.bid,b.bname from a
left join b
on a.id=b.aid
条件弄掉了select
a.*,b.bid,b.bname
from
a
left join
b
on
a.id = b.aid
where
not exists(select 1 from tb where b.aid = aid qnd id < b.id) and b.bid = 123
--======= 每天都在进步,却依然追不上地球的自传=========
--======= By: zc_0101 At:2009-08-20 13:09:48=========
--========++++++++++++++++++++++++++++++++++++=========
--> 测试数据: [a]
if object_id('[a]') is not null drop table [a]
go
create table [a] (id int,aname varchar(6))
insert into [a]
select 1,'一杆球' union all
select 2,'二杆球' union all
select 3,'三杆球' union all
select 4,'四杆球' union all
select 5,'五杆球' union all
select 6,'六杆球'
go
--> 测试数据: [b]
if object_id('[b]') is not null drop table [b]
go
create table [b] (id int,aid int,bid int,bname varchar(4))
insert into [b]
select 1,1,123,'金牌' union all
select 2,1,12,'金牌' union all
select 3,2,123,'金牌' union all
select 4,2,13,'金牌' union all
select 5,3,123,'铜牌' union all
select 6,4,123,'金牌' union all
select 7,5,11,'金牌' union all
select 8,6,22,'银牌' union all
select 9,3,33,'金牌' union all
select 10,4,111,'金牌' union all
select 11,4,333,'铜牌'
----------------查询------------
select a.id,a.aname,b.bid,b.bname from [a] a left join [b] b
on a.id=b.aid and b.bid=123--(这里是你要查询的bid)
----------------结果--------------
/*
id aname bid bname
1 一杆球 123 金牌
2 二杆球 123 金牌
3 三杆球 123 铜牌
4 四杆球 123 金牌
5 五杆球 NULL NULL
6 六杆球 NULL NULL
*/
LEFT JOIN B ON A.ID=B.AID AND B.BID='123'
为什么把 and 换成 where 就不能查出空数据呢?
declare @id int
set @id=123--(这里是你要查询的bid)
select a.id,a.aname,isnull(b.bid,@id),b.bname from [a] a left join [b] b
on a.id=b.aid and b.bid=@id
----------------结果--------------
/*
id aname (无列名) bname
1 一杆球 123 金牌
2 二杆球 123 金牌
3 三杆球 123 铜牌
4 四杆球 123 金牌
5 五杆球 123 NULL
6 六杆球 123 NULL
*/
select a.id,a.aname,b.bid,isnull(b.bname,null) from A表 as a,B表 as b
where a.id=b.bid and b.bid=123 group by a.aname