Search code examples
mysqlsqlinner-join

SQL - INNER JOIN of multiple tables is throwing a syntax error


I've done some searching, and re-worked my SQL a number of times based on examples here and from other online sources, but every time I run this SQL I get the same "#1064 - You have an error in your SQL syntax" error.

I'm trying to join four tables using INNER join, and they should always have matching data to key off of (i.e. there should always be one full row for every valid token/token_id). I'm using MySQL version 5.7.26 Here is the query I am trying to run:

   SELECT
    i.name AS invitee_name,
    c.first_name AS child_first,
    c.last_name AS child_last,
    s.invite_status,
    c.avatar
  FROM
    Invites AS i, Tokens AS t, Children AS c, Invite_Statuses AS s
  WHERE
    t.token = sdie02d
  INNER JOIN
    t ON t.token_id = i.token_id
  INNER JOIN
    c ON c.child_id = i.child_id
  INNER JOIN
    s ON s.status_id = i.status_id

The full error I receive (every time) is:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN
    t ON t.token_id = i.token_id
  INNER JOIN
    c ON c.child_id ' at line 11

The tables look like this:

Invites
+---------+----------+----------+-----------+---------------+----------+
| user_id | child_id | token_id | status_id |     email     |   name   |
+---------+----------+----------+-----------+---------------+----------+
|       9 |        2 |        1 |         1 | [email protected] | John Doe |
|       9 |        3 |        2 |         1 | [email protected] | Jane Doe |
+---------+----------+----------+-----------+---------------+----------+
Tokens

+----------+---------+
| token_id |  token  |
+----------+---------+
|        1 | 93kd8i0 |
|        2 | sdie02d |
|        3 | fsj2d9c |
+----------+---------+
Children
+----------+------------+-----------+--------+
| child_id | first_name | last_name | avatar |
+----------+------------+-----------+--------+
|        1 | Timmy      | Johnson   |      4 |
|        2 | Jenny      | Smith     |     32 |
|        3 | Jake       | Jones     |     12 |
+----------+------------+-----------+--------+
Invite_Statuses
+-----------+---------------+
| status_id | invite_status |
+-----------+---------------+
|         1 | invited       |
|         2 | accepted      |
|         3 | rejected      |
+-----------+---------------+

Thanks for any help you can provide.


Solution

  • Multiple errors here

       SELECT
        i.name AS invitee_name,
        c.first_name AS child_first,
        c.last_name AS child_last,
        s.invite_status,
        c.avatar
      FROM
        Invites AS i
      INNER JOIN Tokens AS t
        ON t.token_id = i.token_id
      INNER JOIN Children AS c
        ON c.child_id = i.child_id
      INNER JOIN Invite_Statuses AS s
        ON s.status_id = i.status_id
      WHERE
        t.token = qme34jh