使用in命令创建SQL视图命令

时间:2014-10-05 13:26:28

标签: mysql sql view create-view

我正在用SQL编写一个创建视图程序,我的上一个创建视图语句给了我一些麻烦。对于这个视图声明,我想从哥伦比亚市的所有大学中检索所有的uid。然后我想使用子查询来获取该人的名字和姓氏的信息。官方说明如下

Write a query that returns only the uid value for all universities in the city Columbia. Then use 
that query with an IN sub-query expression to retrieve the first and last names for all people that  
go to school in Columbias

我现在将发布我为create table语句编写的代码,然后我将发布我将用于view语句的两个表。感谢您的帮助。

CREATE VIEW inn AS
SELECT a.uid
FROM letsdoit.university as a
WHERE b.fname, b.lname IN(SELECT b.fname, b.lname FROM letsdoit.person as b WHERE (a.city = "Columbia"));

表格是

               Table "letsdoit.university"
     Column      |         Type          |                        Modifiers                         
-----------------+-----------------------+--------------------------------------
 uid             | integer               | not null default nextval('university) uid_seq'::regclass)
 university_name | character varying(50) | 
 city            | character varying(50) | 



            Table "letsdoit.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

1 个答案:

答案 0 :(得分:0)

根据问题陈述,看起来您指示要创建的第一个查询应该是IN的子查询,并且应该用作查找该大学人员的名字和姓氏的标准(因为UID是在PERSON和大学),所以你的观点是这样的:

create view inn as
select fname, lname
  from letsdoit.person
 where uid in (select uid from letsdoit.university where city = 'Columbia')