Skip to main content

MySQL – NULL values and sorting

Very often we need to sort some data by specific field in db table but in a way that NULL values goes at the end of dataset. Typical example is frontend which displays data from specific table, with sortable links at the top where you can make asc/desc sorting simple with one click of the mouse.

Since I am mostly dealing with domains, I’ve decided to create one MySQL table with few domains, simple enough to show solution for this problem.

mysql> select * from domains;
+----+-------------------+-------------+
| id | domain            | expire_date |
+----+-------------------+-------------+
|  1 | dinke.net         | 2010-01-17  |
|  2 | lampix.net        | 2009-12-26  |
|  3 | blogodak.com      | 2010-09-08  |
|  4 | maestrodesert.com | 2009-09-11  |
|  5 | unexistant.com    | NULL        |
+----+-------------------+-------------+
5 rows in set (0.00 sec)

So problem is, I want to sort data by expire_data field but in a way that NULL field (ie domain which is not regged yet or has expired) always goes at the end. By default NULL fields goes on the top if we sort by acceding (asc) order or at the end if we sort in descending (desc) order.

mysql> select * from domains
order by expire_date asc;
+----+-------------------+-------------+
| id | domain            | expire_date |
+----+-------------------+-------------+
|  5 | unexistant.com    | NULL        |
|  4 | maestrodesert.com | 2009-09-11  |
|  2 | lampix.net        | 2009-12-26  |
|  1 | dinke.net         | 2010-01-17  |
|  3 | blogodak.com      | 2010-09-08  |
+----+-------------------+-------------+
5 rows in set (0.00 sec)

mysql> select * from domains
order by expire_date desc;
+----+-------------------+-------------+
| id | domain            | expire_date |
+----+-------------------+-------------+
|  3 | blogodak.com      | 2010-09-08  |
|  1 | dinke.net         | 2010-01-17  |
|  2 | lampix.net        | 2009-12-26  |
|  4 | maestrodesert.com | 2009-09-11  |
|  5 | unexistant.com    | NULL        |
+----+-------------------+-------------+
5 rows in set (0.00 sec)

We are going to solve this problem by using MySQL IF function, and solution is:

mysql> select * from domains
order by if(expire_date is null, 1, 0), expire_date asc;
+----+-------------------+-------------+
| id | domain            | expire_date |
+----+-------------------+-------------+
|  4 | maestrodesert.com | 2009-09-11  |
|  2 | lampix.net        | 2009-12-26  |
|  1 | dinke.net         | 2010-01-17  |
|  3 | blogodak.com      | 2010-09-08  |
|  5 | unexistant.com    | NULL        |
+----+-------------------+-------------+
5 rows in set (0.00 sec)

MySQL IF function is similar as ternary operator, it returns value of first argument in case when expression evaluate to true, otherwise it returns value of 2nd argument. So in this particular example it returns 1 for NULL values and 0 for the rest, allowing us to have NULL values at the end of the list.

I know that this might sound little bit confusing so we have to go one step further and add another one field in our MySQL table in order to explain what is exactly going on here.

mysql> alter table domains
add column nullorder tinyint not null;
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

then we are going to update values of nullorder field so they contains values of IF expression above:

mysql> update domains
set nullorder = if(expire_date is null, 1, 0);
Query OK, 1 row affected (0.00 sec)
Rows matched: 5  Changed: 1  Warnings: 0

mysql> select * from domains;
+----+-------------------+-------------+-----------+
| id | domain            | expire_date | nullorder |
+----+-------------------+-------------+-----------+
|  1 | dinke.net         | 2010-01-17  |         0 |
|  2 | lampix.net        | 2009-12-26  |         0 |
|  3 | blogodak.com      | 2010-09-08  |         0 |
|  4 | maestrodesert.com | 2009-09-11  |         0 |
|  5 | unexistant.com    | NULL        |         1 |
+----+-------------------+-------------+-----------+
5 rows in set (0.00 sec)

Now when we have nullorder field our first query:

select * from domains
order by if(expire_date is null, 1, 0), expire_date asc;

can also be written in this way:

mysql> select * from domains
order by nullorder, expire_date;
+----+-------------------+-------------+-----------+
| id | domain            | expire_date | nullorder |
+----+-------------------+-------------+-----------+
|  4 | maestrodesert.com | 2009-09-11  |         0 |
|  2 | lampix.net        | 2009-12-26  |         0 |
|  1 | dinke.net         | 2010-01-17  |         0 |
|  3 | blogodak.com      | 2010-09-08  |         0 |
|  5 | unexistant.com    | NULL        |         1 |
+----+-------------------+-------------+-----------+
5 rows in set (0.00 sec)

So as you can see completely the same stuff as first one, except that we didn’t have to create nullorder field.

Of course in similar way you can have NULL fields appear at the beginning of the list (in case of desc order) when you need something like that.

Future of Web Applications, Miami

Tuesday, cloudy day in Miami, not really good weather for the beach, but who cares. Today is FOWA day!

Check in was within 08-09AM in beautiful Adrienne Arsht arena. Checked location on Google maps, it’s just nearby my hotel so no need to deal with car. Arrived just few minutes before 09AM, so after they checked my data they gave me blank pass and pen to write down my name/company on it. As every proud Serb I took the chance to draw Serbian flag there so my geek colleagues knows exactly from where I am 🙂

Inside of hall there was bunch of geeks from all colors and ages … but most of them have one thing in common. They were all armed with a Macbook. I counted like 50 Macbook (Pro) laptops, with only one sad PC guy. I joined the mass with my Macbook Pro and started with some kind of live broadcasting to well known Serbian Development Forum.

Fowa, Miami

Before he started his intro speech, Ryan Carson asked from us to meet and shake hands with each others. Most of us were doing that … “Hey bro, how yo doin’ …” and suddenly robots becomes a man. Well done Ryan.

First guy on the stage was Jason Fried who were talking about their Getting Real philosophy, without much noise and any presentation screen behind. He mentioned plenty of good advices regarding productivity, sales, future prediction etc. Jason complained about giving away software for free, because food is not free, our cloth is not free, our car is not free … so why in the world software you made should be free?

Jason (37signals)

After Jason Ajaxian guys (Dion Almaer from Google and Ben Galbraith from Mozilla) took the stage. Their speech was much more interesting to me, with plenty of effective screens behind, describing what is going to happen with browsers in the near future. In one word – revolution, which is going to get current Ajax based applications even more usable with usability closer with those that we have on OS X (they used Mac examples frequently).

Ajaxian guys

After Ajaxian guys, Dan Theurer from Yahoo took the scene. Unfortunately his presentation file was corrupted so somehow he did his speech too fast. Also without presentation slides behind it was really hard to track his speech so many developers took the chance to play with wireless and at the end it seamed that Dan couldn’t wait to move away from the stage which was kind of shame.

After the morning break there was probably the most effective talk made by Joe Stump, lead developer of Digg-a.

Joe Stump

Joe used ingenious slides with his and his brother pics from their childhood in attempt to present developers as lazy, moody and egocentric guys (come on, we all know that it is completely the true), and how to make compact and productive team from them.

My favorite quotes:

Developers are EXTREMELY lazy.

Jedi are rare guys

(finding senior programmers is not easy – demonstrated by picture of kid with light Jedi like sward in hand).

Kristine Halvorson, related to Web Content. Although I don’t think many guys from audience found her speech really interesting (I don’t think developers really care about content), her speech was really original and effective. Kristine also had problems with her presentation, but unlike Yahoo guy she didn’t want to continue till backed guys didn’t fix the problem (meanwhile she made them look really funny). After her speech Kristine asked audience why do we have so little females in hall, which was introduction to their talk about that “issue”.

After they ended Aza Raskin from Mozilla took the stage to tell us about all those great little things Mozilla Lab is preparing for us. He was talking about Ubiquity, kind of Firefox add on which would allow you to do many things, like changing content on pages you’re visiting often (like you could easily replace Google logo with your own ad), and many other things. Unfortunately backed guys screwed it again so Aza also stayed without his presentation and had to leave the stage and finish it at the end of show.

Aza Raskin(Mozilla)

After lunch break Joel Spolsky made an interesting and funny talk, where he talked about programmer’s efficiency, setting working environment, office stuff etc. Like Jason he also mentioned interruption as one of the biggest enemies of programmer’s productivity. He claim (and I totally agree) that every developer during his working day usually has about 2 or 3 hours of “total focus” in which time he is 100% focused on problems he is solving. During that period developer usually don’t feel a time (2 or 3 hours pass like a snap). Noisy environment, phone ringing, even going to toilet are the worst enemies of every programmer. That’s why big companies that are paying attention to those stuff (like Google) are trying to keep developer’s focus as long as possible with ie. keeping his snack near by, the same with toilet and even shower booth 🙂

Talent

After Joel, some very effective (but personally not so interesting) talks was made by Dave McClure and Alex Hunter from Virgin. And after pause there was another one “talk show” with Joel, Jason and Caron, after which Francisco Tolmasky from company 280 North took the stage.

Francisco made great workshop with Cappuccino and Atlas tool. By using Atlas – kind of point and click tool started from the browser on local host, he used point and click interface to create an feed reader application in like 3 minutes! After that he did the same for iPhone platform. I liked whole thing except the fact that you don’t have ability to do kind of “synchronization” between iPhone and Web project (things are kept in separate files). I even took a chance to talk with Francisco after the show about that, who was BTW the only speaker I had a chance to meet personally.

Francisco Tolmasky

At the end there was Gary Vaynerchuk’s talk who somehow acted like a Eminem, he talked loud and fast, sometimes cursing, screaming … I could say almost crying 🙂 Don’t have time to repeat here everything Gary said, so if you have a time, here’s video 🙂


Gary Vaynerchuk at FOWA Miami 09 from DamianVoltes.com on Vimeo.

At the end we were invited to visit the party on Nikki Beach which as far as I can tell many developers did. Free beer provided by Microsoft … you could hardly find something more attractive 🙂

Me on Nikky Beachu

My Personal pics from FOWA conference

P.S. This is English translation of FOWA Conference report made originally in Serbian. Now when I got back to my sucky homeland, I managed to translate it completely. Hopefully you enjoyed.

PHP: Callback functions and OOP

Recently, I had to change default behavior of storing session data into files and use MySQL DB instead. In practice, that means writing whole bunch of callback functions and setting callbacks with session_set_save_handler function. Since I use OOP, what really bothered me was the fact that (according to my PHP CHM Manual sitting on my desktop) for session_set_save_handler, all functions has to exist in global scope, since all callback arguments are strings?

bool session_set_save_handler ( string open, string close, string read, string write, string destroy, string gc )

Doing that in non OOP way with 6 functions on global scope is not something I really liked, so I googled for solution and found that you can easily assign an array like array(‘class_name’, ‘method’) for all callbacks in PHP. Cool stuff which allows you to create session handler class with bunch of static methods for those callbacks, but why the hell that is not documented in PHP Manual???

I went to online manual at least to see if someone submitted comment about this, and find out that session_set_save_handler definition there is completely different:

bool session_set_save_handler ( callback $open, callback $close, callback $read, callback $write, callback $destroy, callback $gc )

Obviously, since last time I browsed online manual, a lot of thing has changed, one among them is introducing “callback” type in those “pseudo types” used only for documentation purposes. And there, manual for callback says following:

callback

Some functions like call_user_func() or usort() accept user defined callback functions as a parameter. Callback functions can not only be simple functions but also object methods including static class methods.

A method of an instantiated object is passed as an array containing an object as the element with index 0 and a method name as the element with index 1.

Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object as the element with index 0.

which basically allows you to pass an array with class name and method as callback, and that method will be called.

Let me give you and example with sessions:

<?php

/**
 * Sessin_Handlers class
 * contains dummy methods needed for session stuff
 * Replace content with some real stuff like db conn etc.
 *
 */
class Session_Handlers
{
	function open($save_path, $session_name)
	{
		echo "Open Method Called<br>";
		return true;
	}

	function close()
	{
		echo "Close Method Called<br>";
		return true;
	}

	function read($id)
	{
		echo "Read Method Called<br>";
		return true;
	}

	function write($id, $sess_data)
	{
		echo "Write Method Called<br>";
		return true;

	}

	function destroy($id)
	{
		echo "Destroy Method Called<br>";
		return true;
	}

	function gc($maxlifetime)
	{
		echo "GC Method Called<br>";
		return true;
	}
}

//call all method from Session_Handlers statically
session_set_save_handler(array('Session_Handlers', 'open'), array('Session_Handlers', 'close'), array('Session_Handlers', 'read'), array('Session_Handlers', 'write'), array('Session_Handlers', 'destroy'), array('Session_Handlers', 'gc'));

session_start();

// proceed to use sessions normally
?>

As you see, we’ve created simple methods which only echo when they are called (in real life, you should either save session data into file or db). As you can see, we simple passed arrays to session_set_save_handler, which served us to connect class methods with session callbacks.