有下面的2个表,想写一个查询语句。
-- Create table
create table T_SALARY
(
zgh VARCHAR2(40),
fgznf VARCHAR2(4),
fgzyf VARCHAR2(2),
A1 VARCHAR2(20) default 0.00,
A2 VARCHAR2(20) default 0.00,
A3 VARCHAR2(20) default 0.00,
A4 VARCHAR2(20) default 0.00,
A5 VARCHAR2(20) default 0.00
)
INSERT INTO T_SALARY VALUES('95001','2014','1','11.00','12.00','13.00','14.00','15.00');
INSERT INTO T_SALARY VALUES('95001','2014','2','21.00','22.00','23.00','24.00','25.00');
INSERT INTO T_SALARY VALUES('95001','2014','3','31.00','32.00','33.00','34.00','35.00');
-- Create table
create table T_TABLE_ITEMS
(
wid VARCHAR2(40) not null,
col_name VARCHAR2(40) not null,
col_chname VARCHAR2(100),
is_use VARCHAR2(1)
)
INSERT INTO T_TABLE_ITEMS VALUES('1','A1','内退工资','1');
INSERT INTO T_TABLE_ITEMS VALUES('2','A2','岗位工资','1');
INSERT INTO T_TABLE_ITEMS VALUES('3','A3','薪级工资','1');
INSERT INTO T_TABLE_ITEMS VALUES('4','A4','院长基金10%','1');
INSERT INTO T_TABLE_ITEMS VALUES('5','A5','房贴','0');
现在的语句是:
select t.col_name,t.col_chname,(select t.col_name from T_SALARY a where a.zgh='amadmin' and a.fgznf='2014' and a.fgzyf='2') as value from T_TABLE_ITEMS t where t.is_use='1' and t.col_type='1'
得到的结果是:
主要是VALUE那一列,为什么查询到的值不能再次作为查询条件呢,而是被当做结果直接输出了?
------解决方案--------------------
你不会是这种意思吧
select t.col_name,
t.col_chname,
(select decode(t.col_name,'A1',a1,'A2',a2,'A3',a3,'A4',a4,a5)
from T_SALARY a
where a.zgh = '95001'
and a.fgznf = '2014'
and a.fgzyf = '2') as value
from T_TABLE_ITEMS t
where t.is_use = '1'
很局限的