内部连接多个表

时间:2012-03-15 20:47:19

标签: mysql

我正在尝试将其他表加入到我的查询中,并遇到了此错误:

  

不唯一的表/别名:'events'

发生在我添加的时候:

events INNER JOIN party_types
ON events.PARTY_TYPE = party_types.ID

我的代码现在看起来像这样:

SELECT
    events.EVENT_NAME, events.start_datetime, events.end_datetime, events.VENUE_LOCATION, events.PARTY_TYPE, events.IMAGE_URL, 
    venues.VENUE_NAME, 
    party_types.PARTYTYPE
FROM
    events INNER JOIN venues 
    events INNER JOIN party_types
    ON events.VENUE_LOCATION = venues.ID
    ON events.PARTY_TYPE = party_types.ID
WHERE
    start_datetime >= '$DATE_START_SELECTED' and end_datetime < '$DATE_END_SELECTED

为什么会这样?谢谢:))

1 个答案:

答案 0 :(得分:4)

这不是有效的语法。它被解释如下:

FROM events
INNER JOIN venues AS events
INNER JOIN party_types

第二个events被解释为venues表的别名。 AS是可选的。

请改为尝试:

SELECT
    events.EVENT_NAME,
    events.start_datetime,
    events.end_datetime,
    events.VENUE_LOCATION,
    events.PARTY_TYPE,
    events.IMAGE_URL, 
    venues.VENUE_NAME, 
    party_types.PARTYTYPE
FROM events
INNER JOIN venues 
    ON events.VENUE_LOCATION = venues.ID
INNER JOIN party_types
    ON events.PARTY_TYPE = party_types.ID
WHERE start_datetime >= '$DATE_START_SELECTED'
AND end_datetime < '$DATE_END_SELECTED'