2014-10-05 55 views
0

我正在用SQL編寫一個創建視圖程序,而我上一次創建視圖語句給了我一些麻煩。對於這個觀點陳述,我想從哥倫比亞市的所有大學中找回所有的使用者。然後我想使用子查詢來獲取該人的名字和姓氏的信息。官方的說明如下使用in命令創建一個SQL視圖命令

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 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 

回答

0

基於問題的陳述,它看起來像你將被要求創建應該是在子查詢第一個查詢,並應以此爲標準來發現這個大學的人名(因爲UID在人和大學),所以你的觀點是這樣的:

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