select u.name,o.name,affiliation
from user u,org o 
where o.org_id = u.org_id 
and o.state = '1'
and u.state = '1'
and (
      (o.identity ='11101057601' and u.user_id = '123456' and affiliation = '1222') 
      or (o.identity ='00000' and u.user_id = '10000000' and affiliation = '5555')
)
大概是这么个感觉,affiliation是别的数据库中查出来的值,我放到查询参数中,想要随着查询结果一并查出。
user和org两张表中没有affiliation字段,所以会报错,怎么能声明affiliation为临时字段好查出结果或别的什么方法?原sql是用ibatis+mysql写的:
select u.name as user_name,o.name as org_name,affiliation
from user u,org o 
where o.org_id = u.org_id 
and o.state = '1'
and u.state = '1'
and (
<iterate property="member" open="(" close=")" conjunction=" or ">
                     o.identity = $member[].nsrsbh$ 
                              and u.user_id = $member[].user_id$ 
                              and affiliation = $member[].user_id$
</iterate>
)

解决方案 »

  1.   

    affiliation声明为变量
    select 别的数据库中查出来的值 into affiliation from ....
    再运行你的SQL语句
      

  2.   

    affiliation声明为变量
    select 别的数据库中查出来的值 into affiliation from ....
    再运行你的SQL语句
      

  3.   

    select u.name,o.name,(select xxxxx from tb )affiliation
    from user u,org o  
    where o.org_id = u.org_id  
    and o.state = '1'
    and u.state = '1'
    and (
      (o.identity ='11101057601' and u.user_id = '123456' and affiliation = '1222')  
      or (o.identity ='00000' and u.user_id = '10000000' and affiliation = '5555')
    )
      

  4.   

    谢谢楼上两位回答我的问题,但是小弟都没看懂....可否再指点一下?怎么声明affiliation为变量?不用存储过程
      

  5.   

    select 别的数据库中查出来的值 into @affiliation from ....;
    select u.name,o.name,@affiliation
    from user u,org o  
    where o.org_id = u.org_id  
    and o.state = '1'
    and u.state = '1'
    and (
       (o.identity ='11101057601' and u.user_id = '123456' and affiliation = '1222')  
       or (o.identity ='00000' and u.user_id = '10000000' and affiliation = '5555')
    );
    如果affiliation中的值是1个
    select u.name,o.name,(select 别的数据库中查出来的值 from ...) as affiliation
    from user u,org o  
    where o.org_id = u.org_id  
    and o.state = '1'
    and u.state = '1'
    and (
       (o.identity ='11101057601' and u.user_id = '123456' and affiliation = '1222')  
       or (o.identity ='00000' and u.user_id = '10000000' and affiliation = '5555')
    );
      

  6.   

    对org.identity 做下case.
    select u.name,o.name,case when o.identity= '11101057601' then '1222' else '5555' end  affiliation
    from user u,org o  
    where o.org_id = u.org_id  
    and o.state = '1'
    and u.state = '1'
    and (
      (o.identity ='11101057601' and u.user_id = '123456' and affiliation = '1222')  
      or (o.identity ='00000' and u.user_id = '10000000' and affiliation = '5555')
    )