Sunday, June 19, 2011

MySQL Comparing NULL values returns empty set

It has been a little while since I touched some MySQL. Long enough that I forgot about an issue with how MySQL treats NULL values.  What's the issue? You cannot use the typical SQL comparison operators when testing for a NULL value.  It even says so in the MySQL documentation:

http://dev.mysql.com/doc/refman/5.0/en/problems-with-null.html

It states two facts:
  * "In SQL, the NULL value is never true in comparison to any other value, even NULL."
 * "To look for NULL values, you must use the IS NULL test." 


In other words, MySql provides special operators when dealing with NULL values.  So if you were trying to perform a simple query like this:

SELECT id FROM users WHERE email = NULL

you would not get a syntax error. Instead, you just get an empty result returned by the query. Thanks, MySQL. (I sadly spent a good couple of hours tracking this exact problem).  Instead, performing the query like this:

SELECT id FROM users WHERE email IS NULL

will yield the expected results.  There is also IS NOT NULL, and a function IFNULL() to aid in your NULL comparisons.  The documentation is a pretty good read, as mysql handles null quite differently then other languages.

Hopefully this will help save some innocent keyboards being destroyed out of frustration!



3 comments:

  1. I found your blog by pure accident (google "YII game framework"), but wanted to drop a msg and let you know you'r not alone with DB-driven HTML/PHP/Jquery game development. I'v been coding Boardgame Engine on my spare time. Engine consists of server and client with integrated editor and reminds somewhat of 2D birdview RTS engine, allowing player to zoom in/out. It's turnbased, but syncs movements between players by polling server for changes on gameboard. I'm currently studying YII for work project, and decided to recreate my BoardEngine admin-tools with YII (since I have to spend quite sometime keeping admin tools updated). Client is around ~60% complete (well, targetting simple childs boardgame being playable), allowing zooming, pathmapping, path-validation, player-movement and such, but lacking "rule-enforcement". Basically I'm going to create configurable rule-tree, which I call "ActionDialog" (since it reminds somewhat of "choose-your-answer" dialogging when player interacts with menu/dialog)...

    Anyways, nice to see that someone has similar project underway. How far are you with FireTeams?

    ReplyDelete
  2. @Reaktor

    Hey Reaktor! Thanks for stopping by :). It's really awesome to see others using similar technologies to build their projects! Speaking of which, your project sounds very interesting, i'd love to hear more about it. I'm not as far along with FireTeams as I'd like to be.. Life caught up a bit after graduating college but it's going steady. probably about 1/3 of the way completed at this point. I've been participating in the google AI challenge, so that has been taking up my spare time in the last couple weeks :P.

    As you can tell, I haven't been blogging much lately, perhaps I will try to get more into that again as well. Great to hear from you!!

    Brixican-

    ReplyDelete
  3. I have had similar trouble with BoardEngine. Originally it started as AJAX MMORPG back in 2005, which actually came to point in which I had working hack&slash realtime game similar to Travian ("timeline based" & one second cycles for realtime battle), but I was lacking map-engine.

    I had clear vision that I wanted to have "waypoints" which player could travel between, but I didn't know how to make 'em on javascript/html. Finally I went with plain imagemaps on static image... Kind of pathetic that I managed to create ajax mmorpg with weapon crafting and realtime team-battles (graphic/text hybrid little similar to japanese rpg fights), but was unable to come up with way to make scrollable dynamic map... well, my skills were lacking back then on javascript :)

    Anyways, though it seemed great engine and game, it was lacking "the game". It was just "Ultima Online ExtraLite" without anything to do... and at same time I had nervous breakdown with my life & job, and had to take off for three months from all work related. I froze the project (even though I still might have sources + db...)

    At the sametime I started playing boardgames such as Arkham Horror, and realised that these monstrously big board games would work much better onscreen, and I had semi-clear vision of turnbased boardgame with mixture of rpg/strategy... At same time I was working with Jquery & PHP & databases. Add those things, and I finally came up with "customizable boardgame engine" -idea.

    We started making it with my friend. I created the concept & documents & engine-logic (client + server/db), while my friend coded client with Flash... but sadly after ~2 years in making we had written client twice and had trouble keeping up with life & daywork. Mainly my friend was struggling with working and studying at sametime, thus not keeping up with client as fast as I wanted to code server. When my friend was unable to keep up working with Flash Client, I decided to take all strings to my hands and port Flash engine to Jquery, and thats where I'm at now...

    I'm not really having any hurry completing project, because my first son was born 2 months ago, we moved to another city and I just started new job with new projects, but I'm working on BoardEngine whenever I feel inspired to do so, such as now at new job while I'm studying Yii-framework :)

    I guess everything finally comes to evaluating "can you do it alone", and if the answer is "Yes, I can do first release alone" then the project can eventually be finished... I guess someone said "keep it simple stupid" ;)

    Well in case you ever want to test my build, you can contact me via reaktor8@gmail.com . My development blog is in Finnish, so I guess nothing to see there, and no website before I have something really playable ready to present.

    ReplyDelete