日期:2014-05-18  浏览次数:20558 次

求sql语句??????????????????????
求视图:
表:table1  
  id           fieldname       dy_field             val
  10             dw_id                 dw                   1001
  56             km_id                 km                   9423
  12             dw_id                 dw                   2002

表:table2
km_id         dw_id                   money
9423           1001                   123.00  
9424           2002                   300.00
9423           2002                   500.00
9425           1001                   330.00
求一个视图,查询结果为(表1中全有的数据不能查出来)
km_id         dw_id         money
9424             2002           300.00
9425             1001           330.00


------解决方案--------------------
好像不是这个意思

select * from table2 a
where not (
exists (
select 1 from table1
where val=a.km_id
and fieldname= 'km_id '
)
and exists (
select 1 from table1
where val=a.dw_id
and fieldname= 'dw_id '
)
)

------解决方案--------------------
--try:

create table t1(id int,fieldname varchar(10),dy_field varchar(05),val int)
insert into t1
select 10, 'dw_id ', 'dw ',1001 union all
select 56, 'km_id ', 'km ',9423 union all
select 12, 'dw_id ', 'dw ' ,2002

create table t2(km_id int,dw_id int,[money] int)
insert into t2
select 9423,1001,123 union all
select 9424,2002,300 union all
select 9423,2002,500 union all
select 9425,1001,330

select * from t2
where not (exists(select 1 from t1 where t1.val=t2.km_id and t1.fieldname= 'km_id ')
and exists (select 1 from t1 where t1.val=t2.dw_id and t1.fieldname= 'dw_id '))

drop table t1,t2

/*
km_id dw_id money
----------- ----------- -----------
9424 2002 300
9425 1001 330
*/