首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

两张关联表,怎么显示一条记录,sql怎么写

2012-01-29 
两张关联表,如何显示一条记录,sql如何写?表aid,name1a2c表b(fid与表a的id关联)id fid,n1,n2,b1(bit型)11aa

两张关联表,如何显示一条记录,sql如何写?
表a
 id,name
  1 a
  2 c

 表b (fid与表a的id关联)

 id fid,n1,n2,b1(bit型)
  1 1 aa bb 0
  2 1 cc dd 1
  3 1 ee ff 1
  4 2 ss xx 0
  5 2 yy zz 0
  

要显示如下形式

  a.id a.name,b.n1,b.n2
  1 a ee ff
  2 c yy zz  

也就是 全显示出表A中记录,然后在b表中找出b1为1(没有就是0),id最大的一条记录与之相连形成一条记录显示

如何写sql?先谢了

 

[解决办法]

SQL code
--按某一字段分组取最大(小)值所在行的数据--2007-10-23于杭州/*数据如下:name val memoa    2   a2(a的第二个值)a    1   a1--a的第一个值a    3   a3:a的第三个值b    1   b1--b的第一个值b    3   b3:b的第三个值b    2   b2b2b2b2b    4   b4b4b    5   b5b5b5b5b5*/--创建表并插入数据:create table tb(name varchar(10),val int,memo varchar(20))insert into tb values('a',    2,   'a2(a的第二个值)')insert into tb values('a',    1,   'a1--a的第一个值')insert into tb values('a',    3,   'a3:a的第三个值')insert into tb values('b',    1,   'b1--b的第一个值')insert into tb values('b',    3,   'b3:b的第三个值')insert into tb values('b',    2,   'b2b2b2b2')insert into tb values('b',    4,   'b4b4')insert into tb values('b',    5,   'b5b5b5b5b5')go--一、按name分组取val最大的值所在行的数据。--方法1:select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name--方法2:select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)--方法3:select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name--方法4:select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name--方法5select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name/*name       val         memo                 ---------- ----------- -------------------- a          3           a3:a的第三个值b          5           b5b5b5b5b5*/--二、按name分组取val最小的值所在行的数据。--方法1:select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name--方法2:select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)--方法3:select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name--方法4:select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name--方法5select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name/*name       val         memo                 ---------- ----------- -------------------- a          1           a1--a的第一个值b          1           b1--b的第一个值*/--三、按name分组取第一次出现的行所在的数据。select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name/*name       val         memo                 ---------- ----------- -------------------- a          2           a2(a的第二个值)b          1           b1--b的第一个值*/--四、按name分组随机取一条数据。select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name/*name       val         memo                 ---------- ----------- -------------------- a          1           a1--a的第一个值b          5           b5b5b5b5b5*/--五、按name分组取最小的两个(N个)valselect a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.valselect a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.valselect a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name/*name       val         memo                 ---------- ----------- -------------------- a          1           a1--a的第一个值a          2           a2(a的第二个值)b          1           b1--b的第一个值b          2           b2b2b2b2*/--六、按name分组取最大的两个(N个)valselect a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.valselect a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.valselect a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name/*name       val         memo                 ---------- ----------- -------------------- a          2           a2(a的第二个值)a          3           a3:a的第三个值b          4           b4b4b          5           b5b5b5b5b5*/ 


[解决办法]

SQL code
select a.* , isnull(t.n1,'') n1,isnull(t.n2,'') n2 from a left join(select t.* from b where id = (select max(id) from tb where fid = t.fid)) ton a.id = t.fid
[解决办法]
SQL code
select t1.*,t2.n1,t2.n2 from  a t1left join(select * from b a where not exists(select 1 from b where a.fid=fid and id>a.id))t2on t1.id=t2.fid
[解决办法]
SQL code
 
create table A(id int,name varchar(10))
insert into A select 1,'a'
insert into A select 2,'c'
insert into A select 3,'e'

create table B(id int identity(1,1),fid int,n1 varchar(10),n2 varchar(10),b1 bit)
insert into B select 1,'aa','bb',0
insert into B select 1,'cc','dd',1
insert into B select 1,'ee','ff',1  --this
insert into B select 2,'ss','xx',0
insert into B select 2,'yy','zz',0  --this
insert into B select 3,'gg','hh',0
insert into B select 3,'kk','jj',1  --this
insert into B select 3,'qq','ss',0


select A.id,A.name,AA.n1,AA.n2
from A
left join
(
select t1.*
from B t1
inner join
(
select fid,max(id) as id
from B
where (exists(select 1 from B x where x.fid=B.fid and x.b1=1) and b1=1)
Or (not exists(select 1 from B y where y.fid=B.fid and y.b1=1) and b1=0)
group by fid
) t2
on t1.id=t2.id
) AA
on A.id=AA.fid

/*
id      name    n1    n2   
----------- ---------- ---------- ----------
1      a      ee    ff
2      c      yy    zz
3      e      kk    jj
*/


drop table A,B

热点排行