Search code examples
mysqlsqlunexpected-token

SQL (phpMyAdmin) unexpected dot in ON statement


I have created a view in my database called 'people' that retrieve data using three functions called 'isUserVerified', 'hasUserPicture' and 'userHobbies' from two tables called 'users' and 'user_hobbies':

SELECT 
    `people`.`users`.`id` AS `id`,
    `isUserVerified`(`people`.`users`.`id`) AS `verification`,
    `hasUserPicture`(`people`.`users`.`id`) AS `profile_picture`,
    `userHobbies`(`people`.`user_hobbies`.`user_id`) AS `hobbies` 
FROM 
    (`people`.`users` INNER JOIN `people`.`user_hobbies`) 
ON 
    `people`.`user_hobbies`.`user_id` = `people`.`user_hobbies`.`user_id` 
WHERE 
    `people`.`user_hobbies`.`user_id` = `people`.`user_hobbies`.`user_id` 

phpMyAdmin is complaining an unexpected dot (the last one in the following lines (before user_id):

ON 
    `people`.`user_hobbies`.`user_id` =

and an unexpected token before the = in the same lines above. Is the JOIN the correct way to retrieve data from two table considering that my view uses function that connect to the relevant tables? If yes can anyone tell me what's wrong with my SQL. Thank you


Solution

  • Your syntax is incorrect; the FROM clause should not be enclosed in parentheses. Also, your JOIN condition should probably be

    `people`.`user_hobbies`.`user_id` = `people`.`users`.`id`
    

    and your WHERE condition is redundant as it is the same as the JOIN condition. Try this instead (note that I have aliased the table names to simplify the query):

    SELECT 
        `u`.`id` AS `id`,
        `isUserVerified`(`u`.`id`) AS `verification`,
        `hasUserPicture`(`u`) AS `profile_picture`,
        `userHobbies`(`h`.`user_id`) AS `hobbies` 
    FROM 
        `people`.`users` u
    INNER JOIN
        `people`.`user_hobbies` h
    ON 
        `h`.`user_id` = `u`.`id`