简体   繁体   中英

MySQL select statement giving error

This is the MySQL select statement I have that's giving me the error - Unknown column 'Regattas.regatta_id' in 'on clause'

    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}

The layout of the tables are like the below:

Regattas Table:

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

Events Table:

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

And the Regatta_Events table like this - Conjunction table:

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

Please Help me fix this I have been on it for a while.

Assuming this is what you're trying to do, you can use UNION ALL for this. You don't need the OUTER JOIN with the first query -- I left it just for reference (not 100% positive what you're trying to achieve):

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 

I'm not completely sure I understand your desired results. This will return all results from the Events table, and only those results from the Regattas table where the id matches in the input.

Perhaps instead you are looking for something like this:

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}

I think this should be simple join to return you Regetta_id, event_ID and event_name combination.

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM