MySQL Select Columns Quirk

A long time ago I came across a quirk with MySQL that would not let you select all columns in a query  if you first specify a column name.  So executing the following SQL:

select post_title, * from posts;

would throw a syntax error:

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 posts' at line 1

Specifying the column after the * does work though:

select *, post_title from posts;

For a while this caused a problem especially when testing queries, but then I realised that to get round this quirk you can give the table an alias and then use the alias to select all columns:

select post_title, p.* from posts p;
This entry was posted in Programming and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

* = Required