2013-09-01 37 views
2

在下面的示例SQL表別名範圍Informix中SQL

select * from (
    select col 
    from myTable mt 
    inner join anotherMyTable amt on mt.id = amt.id 
) as t 
where 
    exists (select amt.colX from anotherMyTable amt where amt.id = 42) 

在「AMT」別名在兩個地方所定義。聲明與第一個表名相同的第二個表別名是正確的還是我應該使用其他名稱(amt2)?

在這個例子中,我假設這兩個別名都位於不同的作用域中,所以可以使用相同的名稱。我使用Informix DBMS。

p.s.這是一個示例SQL,問題只是關於表別名範圍。

回答

1

在此範圍:

exists (select amt.colX from anotherMyTable amt where amt.id = 42) 

沒有定義AMT的別名,所以你被允許使用這個別名。下面

兩個例子都是錯誤的:

select * from (
    select col 
    from myTable amt 
    inner join anotherMyTable amt on amt.id = amt.id 
) as t 
where 
    exists (select amt.colX from anotherMyTable amt where amt.id = 42) 


select * from (
    select col 
    from myTable mt 
    inner join anotherMyTable amt on mt.id = amt.id 
) as amt 
where 
    exists (select amt.colX from anotherMyTable amt where amt.id = 42)