March 3, 2012

What am I missing in this SQL?

Question by exculuber

I’ve written some SQL for a MySQL database.

SELECT U.Id, U.Name, U.Surname 
  FROM match M 
 INNER JOIN created_by C 
    ON C.MatchId = M.Id 
 INNER JOIN user U 
    ON U.Id = C.UserId 
 WHERE M.Id = 3

I’m going crazy because it doesn’t seem wrong but the interpreter says there is a syntax error here near 'match M INNER JOIN created_by C ON C.MatchId=M.Id INNER JOIN user U O'.

Thanks for any advice.

Answer by Michael Berkowski

MATCH is a MySQL reserved keyword. Enclose it in backticks if you intend to use it as a column or table name.

SELECT U.Id, U.Name, U.Surname 
  FROM `match` M 

Answer by Starx

I think user & match is an reserved keyword. You need to escape it. Try the following query

SELECT U.Id, U.Name, U.Surname 
  FROM `match` M 
 INNER JOIN `created_by` C 
    ON C.MatchId = M.Id 
 INNER JOIN `user` U 
    ON U.Id = C.UserId 
 WHERE M.Id = 3

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!