Home > Software engineering >  I'm trying to create a SQL query using an inner join but I'm getting a syntax error for my
I'm trying to create a SQL query using an inner join but I'm getting a syntax error for my

Time:04-26

SELECT 
    u.userId, u.firstName, u.lastName, u.emailAddress, 
    u.phoneNumber, ap.date, ap.time, a.balance 
FROM 
    users u 
WHERE 
    u.firstName = 'first_name' 
    AND u.lastName = 'last_name' 
    AND u.emailAddress = '[email protected]' 
INNER JOIN 
    accounts a ON u.userId =  a.userId 
INNER JOIN 
    appointments ap ON u.userId = ap.userId;

CodePudding user response:

Your query syntax sequence is wrong. If you want to JOIN any table it should come after your parent table.

SELECT u.userId, u.firstName, u.lastName, u.emailAddress, u.phoneNumber, ap.date, ap.time, a.balance 
FROM users u 
INNER JOIN accounts a ON u.userId = a.userId 
INNER JOIN appointments ap ON u.userId = ap.userId
WHERE u.firstName = 'first_name' AND u.lastName = 'last_name' AND u.emailAddress = '[email protected]';

Check on https://www.w3schools.com/mysql/mysql_join.asp

CodePudding user response:

Your JOIN statements go before the WHERE clause:

SELECT
    u.userId,
    u.firstName,
    u.lastName,
    u.emailAddress,
    u.phoneNumber,
    ap.date,
    ap.time,
    a.balance 
FROM
    users u  
    INNER JOIN accounts a ON u.userId =  a.userId 
    INNER JOIN appointments ap ON u.userId = ap.userId
WHERE
    u.firstName = 'first_name'
    AND u.lastName = 'last_name'
    AND u.emailAddress = '[email protected]';
  •  Tags:  
  • sql
  • Related