I rarely have to double order my resultant rows in most projects. I had to look this up however, to understand how to order my rows in, not one, but two different ways. It turns out that’s it easy and pretty neat.
Let’s say you have two columns that describe two common pieces of data, first names and last names. If you wanted to order everyone by first name, it would be done easily by ORDER BY first_name
. But then, if you wanted also to have those names in order by last name too? Easy. Simply add a comma: ORDER BY first_name, last_name
.
But there’s more! For instance, if you wanted, you could order the last names in ascending order (A through Z) but have the first names in descending order (Z through A) like so, ORDER BY last_name ASC, first_name DESC
. Each column can have it’s own ordering direction.
If you can add two columns, you can also add three, or four, or more. For instance, like the name ordering scheme previously, adding in age to further order these people is possible: ORDER BY first_name, last_name, age
.
That’s all there is to ordering multiple columns in mysql.