有表table1,table2
关系如下:
table1              table2
A   B   C           B   F其中table1中B字段允许为空
我写了如下几中2表查询的语句
select F as [所需显示名] from table1 left join table2 on table1.B = table2.B where A = 'aaa'        --这样正确
但是我考虑到如果table1中有多个字段是外部键的话,假如字段C也是另一个表的主关键字,那么通过left join就很麻烦了
我想这样写
Select (select F from table2 where table1.B = table2.B) as [所需显示名]
from table1 where A = 'aaa'
如果A = 'aaa' 有多条记录,且有一条中B为null上面的语句就出现所有[所需显示名]为空,我觉得很费解因为如果B为null那么select F from table2 where table1.B = table2.B为null 也只这条记录为null怎么会所有记录为null
我想执行顺序是不是先执行select F from table2 where table1.B = table2.B再执行整个,但是也不好解释,另外我还发现如果不要where A= 'aaa'那么选出的结果符合条件,不知道为什么,加上就不可以了.
请哪位高手指点下!!!

解决方案 »

  1.   

    这么理解
    Select (select F from table2 where table1.B = table2.B) as [所需显示名]
    from table1 where A = 'aaa'如果(select F from table2 where table1.B = table2.B)没有返回值,sql也是返回null
      

  2.   

    如果A = 'aaa' 有多条记录,且有一条中B为null上面的语句就出现所有[所需显示名]为空不会出现这种情况吧
      

  3.   

    declare @table1 table (
    A varchar(10),
    B int,
    c int
    )declare @table2 table (
    B int,
    F int
    )insert @table1 
    select 'bbb',1,1 union all
    select 'aaa',1,1 union all
    select 'aaa',1,2 union all
    select 'aaa',null,3 union all
    select 'aaa',2,1 union all
    select 'aaa',2,2insert @table2
    select 1,1 union all
    select 2,2 union all
    select 3,4 Select (select F from @table2 where x.B = B) as [所需显示名]
    from @table1 x where A = 'aaa'--结果
    所需显示名       
    ----------- 
    1
    1
    NULL
    2
    2(所影响的行数为 5 行)
      
    并不是你说的"所有[所需显示名]为空"
      

  4.   

    刚来,谢谢Haiwer(海阔天空)
    你所说的我都已经试过了,如果是选择出所以记录那么结果如你所叙述
    但是如果A表中有多行记录,我只取其中几条就有问题了
    现在在上班忙点东西,下班后我把那个实例一并写上,请帮忙指点.
      

  5.   

    CREATE TABLE [Oracle_Item_Update_Details] (
    [ID] [int] NOT NULL ,
    [Buyer] [int] NULL

    GOCREATE TABLE [ComboBox_Master] (
    [ID] [int] NOT NULL ,
    [display_Name] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
    )
    GOinsert into Oracle_Item_Update_Details (ID,buyer)values(1,1001)
    insert into Oracle_Item_Update_Details (ID,buyer)values(1,1002)
    insert into Oracle_Item_Update_Details (ID,buyer)values(1,null)
    insert into Oracle_Item_Update_Details (ID,buyer)values(2,1002)
    insert into Oracle_Item_Update_Details (ID,buyer)values(2,1002)insert into ComboBox_Master (ID, display_Name)values(1001,'zhangsan')
    insert into ComboBox_Master (ID, display_Name)values(1002,'lisi')
    --写得不好,见笑,可否告诉我是否有一种可以不通过复制其他表的子查询一下插入多行的写法select Id, (select display_name from ComboBox_Master where ComboBox_Master.Id = Oracle_Item_Update_Details.id) as buyer
    from Oracle_Item_Update_Details
    where id = 1结果:
    1 null
    1 null
    1 null
    附上结果请查看
      

  6.   

    语句里应该是 ComboBox_Master.Id = Oracle_Item_Update_Details.buyer,而不是
    ComboBox_Master.Id = Oracle_Item_Update_Details.id,结果其实还是对的
    select Id, 
    (select display_name from ComboBox_Master where ComboBox_Master.Id = Oracle_Item_Update_Details.buyer)
     as buyer
    from Oracle_Item_Update_Details
    where id = 1Id          buyer
    ----------- --------------------------------------------------
    1           zhangsan
    1           lisi
    1           NULL
      

  7.   

    惭愧,写错了
    不过我本来的并没有错误,我找到错误原因了,不过我不明白为什么会这样
    select (select buyer from (select * from openquery(test,'SELECT agent_name Buyer, agent_id Id FROM po_agents_v ORDER BY Buyer')) as b where
    b.Id = oracle_item_update_details.Nbuyer) as Buyer1,
    (select display_name from combobox_master123 where id = Nbuyer ) as buyer2,
    (select display_name from (select * from combobox_master123) as c where c.id = Nbuyer) as buyer3
    from oracle_item_update_details where ID = 1008
    因为我第一次是把连接查出的数据当成一个表就有问题,而且是通过openquery得出的出问题
    insert into combobox_Master123(display_name,id)
    select * from (select * from openquery(test,'SELECT agent_name Buyer, agent_id FROM po_agents_v ORDER BY Buyer')) as b--导入combobox_Master123
    但是我把这些数据导入到combobox_master123数据是一样就没问题了,何故?
      

  8.   

    结果是
    buyer1 buyer2 buyer3
    null   zhangsan zhangsan
    null   lisi     lisi
    null   null     null
      

  9.   

    我用数据库也有段时间了,oracle 和 MS SQL都有用
    只是这种问题真令我费解
    也就是我把select 连接数据库的记录当成表用就出现差错,但是
    我将这些记录导入到本地数据库的表再查询没错,拿这个本地数据库的表的select 结果当表去查询也没错
    我这么做的目的是通过MS SQL连接oracle数据
    请哪位高手指点!!!
    不管怎么样明天结贴!!!各位辛苦了!