sql从视图和表中选择

时间:2012-09-13 12:44:40

标签: sql view

我有一个select查询,我想在视图和另一个表上进行连接。 视图工作正常,表格也是如此。但是,当我尝试做类似的事情时:

select VIEW.col1, VIEW.col2
from VIEW, TABLE
where VIEW.col1 = TABLE.col1

我得到The multi-part identifier "VIEW.col1" could not be bound.

语法是错误的,还是仅允许在视图上使用?

5 个答案:

答案 0 :(得分:9)

这应该有效

select v.col1, t.col2
from VIEW v, TABLE t
where v.col1 = t.col1

答案 1 :(得分:1)

SELECT VIEW.col1, VIEW.col2
from VIEW AS VIEW INNER JOIN TABLE AS TABLE
ON VIEW.col1 = TABLE.col1

答案 2 :(得分:1)

SELECT MYVIEWNAME.col1, MYVIEWNAME.col2
FROM VIEWNAME AS MYVIEWNAME INNER JOIN TABLENAME AS MYTABLENAME
ON MYVIEWNAME.col1 = MYTABLENAME.col1

答案 3 :(得分:0)

我建议您使用左连接或内连接,并对表和视图进行别名。

select v.col1 , v.col2

 from view v
 inner join table t on t.col1 = v.col1

答案 4 :(得分:0)

您必须view喜欢:

create table MyView
as
select column1 as col1, column2 as col2
from tab

or

CREATE VIEW MyView (col1,col2)
as
select column1, column2
from tab

然后你可以加入它:

select MyView.col1, MyView.col2
from MyView, TABLE
where MyView.col1 = MyView.col1

or

select v.col1, v.col2
from MyView v, TABLE t
where t.col1 = t.col1

or

select v.col1, v.col2
from MyView v
join TABLE t on t.col1 = t.col1

也许某些示例不适用于所有RDBMS。