-1

I am trying to run some MySQL code...

SELECT *,
    event_players - event_claimed AS event_unclaimed
FROM EWRtorneo_events
WHERE event_state = 'visible' AND league_id = 1
    ORDER BY event_date DESC, event_name DESC

When I run this code, I get the following error:

#2014 - Commands out of sync; you can't run this command now

Now this error ONLY shows up when the search results contains a row where the column for event_players is equal to 0. What does this error mean?

If I remove either:

  • event_players - event_claimed AS event_unclaimed
  • ORDER BY event_date DESC, event_name DESC

Then the code starts working. However, both those lines are essential for my function.

This is not a duplicate of other questions, because the circumstances are different.

Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78
  • possible duplicate of [MySQL error #2014 - Commands out of sync; you can't run this command now](http://stackoverflow.com/questions/8891179/mysql-error-2014-commands-out-of-sync-you-cant-run-this-command-now) – BK435 Jul 24 '15 at 19:05

1 Answers1

0

I figured it out... if either of the variables in the subtraction statement in MySQL is an unsigned integer, then the result must always be an unsigned integer as well. If there is ever an instance where the result could be negative, the command will fail.

I solved this by casting the variables as signed:

SELECT *,
    CAST(event_players AS SIGNED) - CAST(event_claimed AS SIGNED) AS event_unclaimed
FROM EWRtorneo_events
WHERE event_state = 'visible' AND league_id = 1
    ORDER BY event_date DESC, event_name DESC
Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78