【问题标题】:sql selecting from view and tablesql 从视图和表中选择
【发布时间】:2012-09-13 12:44:39
【问题描述】:

我有一个选择查询,我想在其中对一个视图和另一个表进行连接。 该视图工作正常,表格也可以单独工作。但是当我尝试做类似的事情时:

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

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

是语法错误,还是视图中不允许这样做?

【问题讨论】:

  • 使用别名会有所帮助
  • 是的。就是这样。现在我记得以前发生过这种情况:) 谢谢

标签: sql view


【解决方案1】:

这应该可以工作

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

【讨论】:

  • 大数据速度慢
【解决方案2】:
SELECT VIEW.col1, VIEW.col2
from VIEW AS VIEW INNER JOIN TABLE AS TABLE
ON VIEW.col1 = TABLE.col1

【讨论】:

    【解决方案3】:
    SELECT MYVIEWNAME.col1, MYVIEWNAME.col2
    FROM VIEWNAME AS MYVIEWNAME INNER JOIN TABLENAME AS MYTABLENAME
    ON MYVIEWNAME.col1 = MYTABLENAME.col1
    

    【讨论】:

      【解决方案4】:

      我建议您使用左连接或内连接并为表和视图设置别名。

      select v.col1 , v.col2
      
       from view v
       inner join table t on t.col1 = v.col1
      

      【讨论】:

        【解决方案5】:

        你必须有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。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-09-01
          • 1970-01-01
          • 2010-09-12
          • 1970-01-01
          • 2020-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多