有A,B两个表,A表中表示丈夫(husbandName不重复),B表表示妻子(wifeName不重复),A和B形成一对多的对应关系,现在求一sql语句,将A中各人在B表中的最漂亮妻子调出来并形成降序排列,没有妻子的人要以0代替
A: husbandName,userpassword
Bob 123445
Bill 52254
Gof 257744
B: wifeName,husbandName,beauty
Kite Bob 5
Sisy Bob 9
Hellen Gof 20
要求出现的结果是:
HusbandName Beauty
Gof 20
Bob 9
Bill 0
参考了帖子:
http://topic.csdn.net/t/20020826/17/971407.html
http://topic.csdn.net/t/20060217/14/4561728.html但无法解决,他们都只是在一个表里面操作,如果两张表怎么弄?
会做的做一下,不会做的顶一下,谢谢大家啦。
11 个解决方案
#1
declare @a table(husbandName varchar(10),userpassword varchar(10))
insert into @a select 'Bob','123445'
insert into @a select 'Bill','52254'
insert into @a select 'Gof','257744'
declare @b table(wifeName varchar(10),husbandName varchar(10) ,beauty int)
insert into @b select 'Kite','Bob',5
insert into @b select 'Sisy','Bob',9
insert into @b select 'Hellen','Gof',20
select a.husbandName,isnull(b.beauty,0) as Beauty from @a a left join
(select * from @b b where not exists(select 1 from @b where husbandName = b.husbandName and beauty > b.beauty))b
on a.husbandName=b.husbandName order by Beauty desc
husbandName Beauty
Gof 20
Bob 9
Bill 0
#2
先丁一下,看看能不能做出来
#3
create table A(husbandName varchar(10))
insert into A values('Bob')
insert into A values('Bill')
insert into A values('Gof')
go
create table B(wifeName varchar(10),husbandName varchar(10),beauty int)
insert into B values('Kity','Bob',5)
insert into B values('Sisy','Bob',9)
insert into B values('Hellen','Gof',20)
go
select h.husbandName,isnull(max(w.beauty),'0') beauty from A h
left join B w on h.husbandName = w.husbandName
group by h.husbandName
order by beauty desc
go
drop table A,B
#4
select
a.*,b.wifeName,isnull(b.beauty,0)
from
a
left join
(select * from b as t where beauty=(select max(beauty) from b where husbandName=t.husbandName))t2
on a. husbandName=t2.husbandName
#5
select a.* , isnull(m.wifeName , '') wifeName , isnull(m.beauty , 0) beauty from a
left join
(
select t.* from b t where beauty = (select max(beauty) from b where husbandName = t.husbandName)
) m
on a.husbandName = m.husbandName
#6
create table a (husbandName varchar(10),userpassword varchar(10))
insert into a select 'Bob','123445'
insert into a select 'Bill','52254'
insert into a select 'Gof','257744'
create table b (wifeName varchar(10),husbandName varchar(10) ,beauty int)
insert into b select 'Kite','Bob',5
insert into b select 'Sisy','Bob',9
insert into b select 'Hellen','Gof',20
select a.* , isnull(m.wifeName , '') wifeName , isnull(m.beauty , 0) beauty from a
left join
(
select t.* from b t where beauty = (select max(beauty) from b where husbandName = t.husbandName)
) m
on a.husbandName = m.husbandName
drop table a,b
/*
husbandName userpassword wifeName beauty
----------- ------------ ---------- -----------
Bob 123445 Sisy 9
Bill 52254 0
Gof 257744 Hellen 20
(所影响的行数为 3 行)
*/
#7
败了,我做不出来了。对不住了
#8
怎么在外面看到回复数是7,进来后一条都没看到?
#9
/*方法1*/
SELECT
[A].[husbandName],[Beauty]=ISNULL(SELECT TOP 1 [Beauty] FROM [B] WHERE [B].[husbandName]=[A].[husbandName],0)
FROM [A] ORDER BY [Beauty] DESC
/*方法2*/
SELECT
[A].[husbandName],[Beauty]=ISNULL([t].[Beauty],0)
FROM [A]
LEFT JOIN
(
SELECT [husbandName],[Beauty]=MAX([Beauty]) FROM [B]
) [t]
ON [A].[husbandName]=[t].[husbandName]
ORDER BY [Beauty] DESC
#10
1楼的在我发帖后13分38秒就做出来了,谢谢,可能可以说是回复最快纪录的啦。感谢大家的支持。
#11
我也有同样的问题
#1
declare @a table(husbandName varchar(10),userpassword varchar(10))
insert into @a select 'Bob','123445'
insert into @a select 'Bill','52254'
insert into @a select 'Gof','257744'
declare @b table(wifeName varchar(10),husbandName varchar(10) ,beauty int)
insert into @b select 'Kite','Bob',5
insert into @b select 'Sisy','Bob',9
insert into @b select 'Hellen','Gof',20
select a.husbandName,isnull(b.beauty,0) as Beauty from @a a left join
(select * from @b b where not exists(select 1 from @b where husbandName = b.husbandName and beauty > b.beauty))b
on a.husbandName=b.husbandName order by Beauty desc
husbandName Beauty
Gof 20
Bob 9
Bill 0
#2
先丁一下,看看能不能做出来
#3
create table A(husbandName varchar(10))
insert into A values('Bob')
insert into A values('Bill')
insert into A values('Gof')
go
create table B(wifeName varchar(10),husbandName varchar(10),beauty int)
insert into B values('Kity','Bob',5)
insert into B values('Sisy','Bob',9)
insert into B values('Hellen','Gof',20)
go
select h.husbandName,isnull(max(w.beauty),'0') beauty from A h
left join B w on h.husbandName = w.husbandName
group by h.husbandName
order by beauty desc
go
drop table A,B
#4
select
a.*,b.wifeName,isnull(b.beauty,0)
from
a
left join
(select * from b as t where beauty=(select max(beauty) from b where husbandName=t.husbandName))t2
on a. husbandName=t2.husbandName
#5
select a.* , isnull(m.wifeName , '') wifeName , isnull(m.beauty , 0) beauty from a
left join
(
select t.* from b t where beauty = (select max(beauty) from b where husbandName = t.husbandName)
) m
on a.husbandName = m.husbandName
#6
create table a (husbandName varchar(10),userpassword varchar(10))
insert into a select 'Bob','123445'
insert into a select 'Bill','52254'
insert into a select 'Gof','257744'
create table b (wifeName varchar(10),husbandName varchar(10) ,beauty int)
insert into b select 'Kite','Bob',5
insert into b select 'Sisy','Bob',9
insert into b select 'Hellen','Gof',20
select a.* , isnull(m.wifeName , '') wifeName , isnull(m.beauty , 0) beauty from a
left join
(
select t.* from b t where beauty = (select max(beauty) from b where husbandName = t.husbandName)
) m
on a.husbandName = m.husbandName
drop table a,b
/*
husbandName userpassword wifeName beauty
----------- ------------ ---------- -----------
Bob 123445 Sisy 9
Bill 52254 0
Gof 257744 Hellen 20
(所影响的行数为 3 行)
*/
#7
败了,我做不出来了。对不住了
#8
怎么在外面看到回复数是7,进来后一条都没看到?
#9
/*方法1*/
SELECT
[A].[husbandName],[Beauty]=ISNULL(SELECT TOP 1 [Beauty] FROM [B] WHERE [B].[husbandName]=[A].[husbandName],0)
FROM [A] ORDER BY [Beauty] DESC
/*方法2*/
SELECT
[A].[husbandName],[Beauty]=ISNULL([t].[Beauty],0)
FROM [A]
LEFT JOIN
(
SELECT [husbandName],[Beauty]=MAX([Beauty]) FROM [B]
) [t]
ON [A].[husbandName]=[t].[husbandName]
ORDER BY [Beauty] DESC
#10
1楼的在我发帖后13分38秒就做出来了,谢谢,可能可以说是回复最快纪录的啦。感谢大家的支持。
#11
我也有同样的问题