1

这是我的视图创建..它没有返回任何行..

CREATE VIEW TOP AS
(
SELECT 
a.USER_ID, a.USER_NAME, a.PASSWORD, a.FIRST_NAME, a.LAST_NAME, a.CONTACT_NUMBER, a.EMERGENCY_NUMBER, a.CITIZEN_SHIP, a.VISA_TYPE, 
b.PLACE_CODE, b.PLACE_NAME, b.AIR_AVAILIBILITY, b.TRAIN_AVAILIBILITY, b.ROAD_AVAILIBILITY, 
c.CLIENT_ID, c.CLIENT_NAME, c.CLIENT_ADDRESS, c.CLIENT_LOCATION,
d.PROJECT_CODE, d.PROJECT_NAME,
e.REQUEST_ID, e.REQUEST_STATUS, e.REQUEST_FOR, e.REQUEST_MODE, e.REQUEST_TYPE, e.TRAVEL_FROM, e.TRAVEL_TO, e.TRAVEL_DATE, e.TRAVEL_TICKET_BY, e.ASSIGNMENT_PLACE, 
e.ASSIGNMENT_COUNTRY, e.TRAVEL_PURPOSE, e.ASSIGNMENT_DURATION, e.ASSIGNMENT_START_DATE, e.ASSIGNMENT_ACCOMODATION_BY, e.ACCOMODATION_BILLABLE, e.TRAVEL_BILLABLE, e.ASSIGNMENT_BILLABLE, e.NSHORE_PROJECT_BILLABLE
FROM USERS a, PLACES b, CLIENTS c, PROJECTS d, REQUESTS e
WHERE 
a.USER_ID = e.USER_ID
AND b.PLACE_CODE = e.TRAVEL_FROM
AND b.PLACE_CODE = e.TRAVEL_TO
AND c.CLIENT_ID = d.CLIENT_ID
AND d.PROJECT_CODE = e.PROJECT_CODE
);

我认为问题出在这两行..

AND b.PLACE_CODE = e.TRAVEL_FROM
AND b.PLACE_CODE = e.TRAVEL_TO

我试图不止一次地引用同一张表。对不起,如果我问的是一个非常基本的问题。我对数据库完全陌生。

4

1 回答 1

0

看起来您正在尝试获取 placeTRAVEL_FROM和 place的单独详细信息TRAVEL_TO。为此,您需要针对具有不同别名的同一个表连接两次。我将用首选FROM的显式 s 替换您的隐式(逗号分隔)连接:INNER JOIN

CREATE VIEW TOP AS
(
SELECT 
  a.USER_ID, a.USER_NAME, a.PASSWORD, a.FIRST_NAME, a.LAST_NAME, a.CONTACT_NUMBER, a.EMERGENCY_NUMBER, a.CITIZEN_SHIP, a.VISA_TYPE, 
  /* one set of columns for TRAVEL_FROM, each with its own alias */
  b_from.PLACE_CODE AS PLACE_CODE_from, b_from.PLACE_NAME AS PLACE_NAME_from, b_from.AIR_AVAILIBILITY AS AIR_AVAILABILITY_from, b_from.TRAIN_AVAILIBILITY AS TRAIN_AVAILABILITY_from, b_from.ROAD_AVAILIBILITY AS ROAD_AVAILABILITY_from, 
  /* and one set for TRAVEL_TO, each with its own alias*/
  b_to.PLACE_CODE AS PLACE_CODE_to, b_to.PLACE_NAME AS PLACE_NAME_to, b_to.AIR_AVAILIBILITY AS AIR_AVAILABILITY_to, b_to.TRAIN_AVAILIBILITY AS TRAIN_AVAILABILITY_to, b_to.ROAD_AVAILIBILITY AS ROAD_AVAILABILITY_to, 
  c.CLIENT_ID, c.CLIENT_NAME, c.CLIENT_ADDRESS, c.CLIENT_LOCATION,
  d.PROJECT_CODE, d.PROJECT_NAME,
  e.REQUEST_ID, e.REQUEST_STATUS, e.REQUEST_FOR, e.REQUEST_MODE, e.REQUEST_TYPE, e.TRAVEL_FROM, e.TRAVEL_TO, e.TRAVEL_DATE, e.TRAVEL_TICKET_BY, e.ASSIGNMENT_PLACE, 
  e.ASSIGNMENT_COUNTRY, e.TRAVEL_PURPOSE, e.ASSIGNMENT_DURATION, e.ASSIGNMENT_START_DATE, e.ASSIGNMENT_ACCOMODATION_BY, e.ACCOMODATION_BILLABLE, e.TRAVEL_BILLABLE, e.ASSIGNMENT_BILLABLE, e.NSHORE_PROJECT_BILLABLE
FROM 
  /* implicit joins replaced with preferred explicit joins */
  USERS a
  INNER JOIN  REQUESTS e ON a.USER_ID = e.USER_ID
  /* Join frist against PLACES for TRAVEL_FROM */
  INNER JOIN PLACES b_from ON b_from.PLACE_CODE = e.TRAVEL_FROM  
  /* And again against PLACES for TRAVEL_TO */
  INNER JOIN PLACES b_to ON b_to.PLACE_CODE = e.TRAVEL_TO
  INNER JOIN PROJECTS d ON d.PROJECT_CODE = e.PROJECT_CODE
  INNER JOIN CLIENTS c ON c.CLIENT_ID = d.CLIENT_ID
);
于 2013-04-13T20:27:54.863 回答