注册 登录
编程论坛 SQL Server论坛

这样的查询语句应该怎么写?

summoner 发布于 2007-05-04 15:30, 863 次点击
一个表有3列,A是主键
A B C
AAA a CCC
BBB b AAA
CCC c BBB
...
现在要这样的查询语句,查询结果中有4列,第4列的值根据C列的值看A列的值对应的B列的值,即结果要这样
A B C D
AAA a CCC c //CCC(C列)-〉CCC(A列)-〉c(B列)
BBB b AAA a //AAA(C列)-〉AAA(A列)-〉a(B列)
CCC c BBB b //BBB(C列)-〉BBB(A列)-〉b(B列)
这条查询语句该怎么写?
6 回复
#2
棉花糖ONE2007-05-04 19:25

if object_id('test') is not null
drop table test
go
create table test(a varchar(10),b varchar(3),c varchar(4))
insert into test select 'AAA','a','CCC'
union all select 'BBB','b','AAA'
union all select 'CCC','c','BBB'

select *,d=(select b from test where a=t.c) from test t

a b c d
---------- ---- ---- ----
AAA a CCC c
BBB b AAA a
CCC c BBB b

#3
summoner2007-05-07 09:51
不行
#4
棉花糖ONE2007-05-07 11:17
我得到的结果不就是你给的吗,不行,哪里有问题
#5
summoner2007-05-07 11:24
ORA-00923: FROM keyword not found where expected
是不是你写的这个只适合于SQL SERVER?
#6
棉花糖ONE2007-05-07 12:26
郁闷我写的是sql server的
#7
summoner2007-05-07 12:35

我用左联写出来了

1