Weird MySQL Behaviour

Last night I added a new log field to PotD and since I did it live on my prod instance, I wrapped it with a bunch of error handling so that if anything went wrong it wouldn't affect users, it just wouldn't log the new data.  (No, this is not my standard operating procedure – PotD is special in this sense).  Because of that silent failure behaviour, I wanted to monitor that the data was recording, so I ran this query:

select isPrioritized, count(*) from log_email;

Now, you can probably see exactly what's wrong with it, but it was late, and I didn't.  What's weirder, MySQL didn't throw an error, it just returned this recordset:

+---------------+----------+
| isPrioritized | count(*) |
+---------------+----------+
|          NULL |    15346 |
+---------------+----------+

So I spent a while trying to figure out what was wrong with my code, and it sure seemed correct.  Eventually gave up and went to bed, figuring that a few more hours without logging the data wouldn't hurt that much.  Upon getting up this morning I quickly realized what the issue was. The right query looks like this:

select isPrioritized, count(*) from log_email group by isPrioritized;

which returns this:

+---------------+----------+
| isPrioritized | count(*) |
+---------------+----------+
|          NULL |    15331 |
|             0 |        3 |
|             1 |       12 |
+---------------+----------+

I'm pretty sure the original query should error because of the aggregate function without a GROUP BY clause, but who knows.  Something to watch out for.

One response to “Weird MySQL Behaviour”

  1. Ben Nadel

    That is odd. Feels like something that should throw an error. I've run into stuff like that a few times where stuff is clearly wrong, but doesn't fail. Once time I had an actual syntax error, but it totally ignored id.