Sometimes when querying MySQL I like to return a new column containing an ordered numerical sequence for each row in the result set. Here’s an explanation of how to do it.

I suppose this isn’t really needed in many, if not most, queries, but sometimes it’s just what I want. As an example, I have a users table that contains three columns: id, first_name and last name. The following query will retrieve a result set from this table where the first_name column is equal to John.

SELECT uuid
FROM users
WHERE email LIKE'%j%';

SELECT uuid
FROM users
WHERE email LIKE '%j%';

We can easily modify this query to insert an additional column to the front of the result set containing a sequential number for each record. Try this:

Voila! And now, how does it work? Let’s start with the FROM clause. We are specifying two sources of data. The first is the users table itself and the second is a subquery that sets a session variable called @ROW and initializes its value to zero. Being a derived table, we must specify an alias, so I used r.

In the SELECT clause, we are incrementing the value of the variable @ROW by one and selecting its value followed by the first_name column from our users table. Since @ROW started life with a value of zero, the first row of the returned result set will be one followed by two, three etc. If we wanted to start at a different value we could’ve set that value in the FROM clause.

And finally, the WHERE clause constrains our results to those whose first name is John. That was pretty easy and quite useful I think.

You can reassign menu commands in Mac’s System Preferences menu to different shortcuts, either for individual applications or for all applications. This will let you effectively reassign Cmd-Q to anything (or nothing), if you’d like.

1. Go to System Preferences, and select Keyboard.

2. Click on Keyboard Shortcuts, then select the Application Shortcuts menu bar in the lower left.

3. Click the “+” button to add a shortcut for an application. For this example, we’ll disable Cmd-Q for Google Chrome.

4. Figure out the command name for Google Chrome’s “Quit” by going to the menu bar in Google Chrome and finding the menu item. In Chrome, under the “Chrome” menu bar element, you can find the action “Quit Google Chrome” attached to Cmd-Q.

5. Put the exact name of the menu action item as a new keyboard shortcut in the Keyboard Shortcut options.