Home > Software design >  A query on the recipesexample database from SQL for mere mortals
A query on the recipesexample database from SQL for mere mortals

Time:10-11

What's the problem with this query? It's only a slight modification from the SQL for mere mortals book...

Select r.RecipeTitle, 
from (Select RecipeClassID
      from recipe_classes as RC
      where RC.RecipeClassDescription like "Main%" 
            or RC.RecipeClassDescription="Dessert") as rcfiltered
inner join recipes as r
on rcfiltered.RecipeClassID = r.RecipeClassID;

CodePudding user response:

Remove the comma:

Select recipes.RecipeTitle, 
from ...

There must be no comma following the last expression in the select-list.

WRONG:

SELECT A, B, C, FROM ...

RIGHT:

SELECT A, B, C FROM ...

With the extra comma, the query produces this error in MySQL:

ERROR 1064 (42000): 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 'from (Select RecipeClassID from recipe_classes as RC where RC.Recipe' at line 2

Here's a tip on how to read syntax error messages: It tells you exactly where the parser got confused: "near 'from ..." This probably means something you wrote in the query immediately prior to that position was wrong. Knowing this helps to narrow down the cause of the syntax error.

If the error says, "near ''" then it means it got to the end of the query and then got confused. Maybe you opened a parenthesis but forgot to close it for example.

That's the first problem that jumps out at me. I don't know the original query you were modifying, so I don't know how you changed it. I don't know if the query does what you intend it to do, but aside from the comma issue it looks like the syntax is valid.

  • Related