1

这是我的 MySQL 选择语句,它给了我错误 - 'on 子句'中的未知列'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}

表的布局如下:

帆船赛表:

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

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

4

2 回答 2

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}
于 2013-07-21T03:05:48.663 回答
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}
于 2013-07-21T03:15:42.463 回答