Note that the way MySQL errors are usually debugged is like so:
$query = 'SELECT blah blah...';
$exec = mysql_query($query) or die('MySQL error: ' . mysql_error() . '<hr>Query: ' . $query);
The MySQL error message usually gives you a pretty good hint as to where in the query it found a problem so that you can go to the MySQL manual for an answer.