SQL - SELECT - FOREIGN KEY

时间:2013-03-21 13:24:15

标签: sql select left-join

VISITSEXIT              VISITSENTRY               VISITOR
idvisitsexit            idvisitsentry             idvisitor 
idvisitsentry           idvisitante               visitor 
idvisitor               visitor
visitor
dateexit
timeexit

我需要选择表VISITSEXIT,当她带来VISITSENTRY时,也会带来idvisitor和访问者(访客)的描述。

1 个答案:

答案 0 :(得分:2)

一个简单直截了当的JOIN会给你你想要的东西:

SELECT
  t.idvisitor,
  t.visitor,
  t.dateexit,
  t.timeext,
  ... -- the rest of the columns you want to select 
FROM VISITSEXIT       AS t
INNER JOIN VisitEntry AS e ON t.idvisitentry = e.visitentry
INNER JOIN Visitor    AS v ON e.idvisitante  = v.idvisitor;

如果要包含其他表中不匹配的行,则可能还需要使用LEFT JOIN。有关JOIN类型的详细信息,请参阅此帖子: