sql语句求助。

2025-04-18 07:59:41
推荐回答(4个)
回答1:

select into,*, decode(a,1,'是',0,'否',a), decode(b,1,'好',0,'坏',b) from infoTable into
这个是oracle里的写法。应该是最简单的。不知道你用什么数据库。其他的可能要用case when判断了

不好意思,很久没过来看。 我现在明白你的意思了。如果是这样就很好做了。
select info.字段1, info.字段2, info.字段3,type1.codename, type2.codename
from infoTable info, codetype type1, codetype type2
where info.a=type1.codetype and info.b=type2.codetype

应该就是这样了。

回答2:

你的表好奇怪,第一个表的主键是什么,第二个表如果使用外键关联的话是要关联前面那个表的主键的呀,像你这样子无法理解

是这样啊,我明白了,这样写或许比较快
select t1.字段1,t1.字段2,t1.字段3,t1.a,(select t2.codename from codetable t2 where t2.code = t1.a and t2.codetype='a' ) as a_name,t1.b,(select t3.codename from codetable t3 where t3.code = t1.b and t3.codetype='b' ) as b_name from infoTable t1

不知是否满你意,在这里用连接(join)查询也可以的,不过写起来比较复杂一点
select t1.字段1,t1.字段2,t1.字段3,t1.a,t2.codename as a_name,t1.b,t3.codename as b_name from (infoTable t1 join codetable t2 on t2.codetype='a' and t2.code = t1.a) join codetable t3 on t3.codetype='b' and t3.code = t1.b

回答3:

select info.字段1, info.字段2, info.字段3, t1.codename, t2.codename
from infoTable info
inner join table1 t1 on info.a=t1.code and t1.codetype='a'
inner join table1 t2 on info.b=t2.code and t2.codetype='b';

回答4:

介绍sql语句如何使用函数