MySQL select语句给出错误

时间:2013-07-21 02:59:50

标签: php mysql

这是MySQL select语句我给出了错误 - 'on clause'中的未知列'Regattas.regatta_id'

    SELECT
        Regattas.regatta_id, 
        Events.event_id,
        Events.event_name
        FROM Regattas, Events
        LEFT JOIN Regatta_Events AS Regatta_Events_1 ON Regatta_Events_1.fk_event_id = Events.event_id 
        LEFT JOIN Regatta_Events AS Regatta_Events_2 ON Regatta_Events_2.fk_regatta_id = Regattas.regatta_id
        WHERE Regattas.regatta_id = {$regattaId}

表格的布局如下:

Regatas表:

+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| regatta_id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| regatta_name       | varchar(100) | NO   |     | NULL    |                |
| regatta_start_date | date         | NO   |     | NULL    |                |
| regatta_end_date   | date         | NO   |     | NULL    |                |
| regatta_start_time | time         | NO   |     | NULL    |                |
| regatta_venue_id   | int(11)      | NO   |     | 0       |                |
+--------------------+--------------+------+-----+---------+----------------+

活动表:

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          | 
+------------+--------------+------+-----+---------+----------------+
| event_id   | int(11)      | NO   | PRI | NULL    | auto_increment |
| event_name | varchar(255) | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

这样的Regatta_Events表 - 连接表:

+-------------------+---------+------+-----+---------+----------------+   
| Field             | Type    | Null | Key | Default | Extra          |
+-------------------+---------+------+-----+---------+----------------+
| regatta_events_id | int(11) | NO   | PRI | NULL    | auto_increment |
| fk_regatta_id     | int(11) | NO   |     | 0       |                |
| fk_event_id       | int(11) | NO   |     | 0       |                |
+-------------------+---------+------+-----+---------+----------------+

请帮我解决这个问题。我已经有一段时间了。

2 个答案:

答案 0 :(得分:1)

假设您正在尝试这样做,可以使用UNION ALL。您不需要OUTER JOIN第一个查询 - 我将其留作参考(不是100%肯定您正在尝试实现的目标):

SELECT
        Regattas.regatta_id, 
        NULL event_id,
        NULL event_name
FROM Regattas
        LEFT JOIN Regatta_Events ON Regatta_Events.fk_regatta_id = Regattas.regatta_id
WHERE Regattas.regatta_id = {$regattaId}
UNION ALL
SELECT
        NULL regatta_id, 
        Events.event_id,
        Events.event_name
FROM Events
        LEFT JOIN Regatta_Events ON Regatta_Events.fk_event_id = Events.event_id 

我不完全确定我理解你想要的结果。这将返回Events表中的所有结果,并且只返回来自Regattas表的结果,其中id在输入中匹配。

也许你正在寻找这样的东西:

SELECT
    Regattas.regatta_id, 
    Events.event_id,
    Events.event_name
FROM Regattas
    LEFT JOIN Regatta_Events ON Regatta_Events.fk_regatta_id=Regattas.regatta_id
    LEFT JOIN Events ON Regatta_Events.fk_event_id=Events.event_id 
WHERE Regattas.regatta_id = {$regattaId}

答案 1 :(得分:0)

我认为这应该是简单的连接,以返回Regetta_id,event_ID和event_name组合。

SELECT
        Regattas.regatta_id, 
        Events.event_id,
        Events.event_name
        FROM Regattas as rg
        LEFT JOIN Regatta_Events as re ON re.fk_event_id = rg.regatta_id 
        LEFT JOIN Events AS ev ON re.fk_regatta_id = ev.event_id
        WHERE rg.regatta_id = {$regattaId}