【问题标题】:Using INTERSECTS to make a create view statement in sql在sql中使用INTERSECTS创建视图语句
【发布时间】:2014-10-05 17:56:28
【问题描述】:

我正在创建一个 SQL 创建视图语句,但我在写最后一条语句时遇到了问题。对于此语句,我必须使用 intersect 来获取 30 岁以上且身高超过 65 英寸的人。查询的具体细节如下

  • 编写一个查询,返回身体成分表中所有年龄超过 30 岁的人的名字和姓氏。然后修改您的查询以使用 INTERSECTS 来返回所有 30 岁以上且身高超过 65 英寸的人。

我尝试使用 intersect 和 inner join 编写此视图语句以从不同的表中获取名称,但是当我尝试运行程序时出现错误。我得到的错误是

错误:“WHERE”处或附近的语法错误
第 11 行:其中 b.height = 65;

我不会发布我的完整程序,只是这个创建视图语句,所以行号将被关闭。我将发布我的代码,然后发布我用来创建此视图语句的两个表,希望有人可以帮助我修复此错误。提前致谢。

CREATE VIEW intersectt AS
   SELECT 
      a.fname, a.lname
   FROM 
      what.person as a
   INNER JOIN 
      what.body_composition as b ON a.pid = b.pid
   WHERE 
      b.age > 30

   INTERSECT

   SELECT 
      a.fname, a.lname
   FROM 
      what.person as a
   INNER JOIN 
      what.body_composition as b 
   WHERE 
      b.height = 65;

这是两张表

what.body_composition

 Column |  Type   | Modifiers 
--------+---------+-----------
 pid    | integer | not null
 height | integer | not null
 weight | integer | not null
 age    | integer | not null

和表what.person

 Column |         Type          |                      Modifiers                      
--------+-----------------------+-----------------------------------------------
 pid    | integer               | not null default nextval('person_pid_seq'::reg class)
 uid    | integer               | 
 fname  | character varying(25) | not null
 lname  | character varying(25) | not null

【问题讨论】:

  • 第二个子查询中没有on 子句。
  • 非常感谢!有没有办法查看我创建的内容?因为我进入 psql 并运行我的 sql 程序 \i what.sql 然后它只是显示表已创建,我想查看表中的内容
  • Sp_help tablename 查看表架构或查看内容

标签: sql view create-table create-view


【解决方案1】:

您在第二个查询中缺少 ON 子句

   CREATE VIEW intersectt AS
   SELECT a.fname, a.lname
   FROM what.person as a
   INNER JOIN     what.body_composition as b
   ON a.pid = b.pid
   AND b.age > 30
   INTERSECT
   SELECT a.fname, a.lname
   FROM what.person as a
   INNER JOIN what.body_composition as b
   ON a.pid = b.pid
   AND b.height = 65 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 2018-03-16
    • 2017-09-23
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多