Why would I use social media to communicate with a business?

November 16th, 2011 1 comment

With the response being public, it’s likely I’ll get something better than “Thanks for your inquiry, we’ll respond to you within 48 hours.”.

Categories: Uncategorized Tags:

MySQL’s handling of GROUP BY

September 28th, 2011 No comments

In the year 2000, I was working on migrating a web application from MySQL to Oracle. One of the issues we had was a number of queries which took advantage of slackness in MySQLs handling of the GROUP BY clause. Oracle took exception to our poor SQL and forced the developers to rewrite queries to be a higher standard. To illustrate the issue, have a look at the following example.

mysql> create table `test1` (`a` int(11), `b` int(11) );
Query OK, 0 rows affected (0.14 sec)

mysql> insert into test1 values (1,2), (1,3);
Query OK, 2 rows affected (0.09 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
|    1 |    3 |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from test1 group by a;
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
+------+------+
1 row in set (0.00 sec)

The issue at hand, is the value for the b column. You’ll note that we’ve grouped by a, so we correctly have a single row result. If you’re not sure what the issue is, let’s repopulate the table in a different order and retry.

mysql> truncate test1;
Query OK, 0 rows affected (0.10 sec)

mysql> insert into test1 values (1,3), (1,2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    3 |
|    1 |    2 |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from test1 group by a;
+------+------+
| a    | b    |
+------+------+
|    1 |    3 |
+------+------+
1 row in set (0.00 sec)

So, the value for b was 2 in the first example, and is now 3, with the same data in the table and the same SQL. The problem is that there is no aggregation or GROUP BY clause for b. There’s only one possible value for a, but b is not aggregated – there are two possible values: 2 or 3. MySQL just picks one of the two possible values. Let’s see what PostgreSQL does.

database=# create temporary table test1 (a integer, b integer);
CREATE TABLE
database=# insert into test1 values (1,2), (1,3);
INSERT 0 2
database=# select * from test1;
 a | b
---+---
 1 | 2
 1 | 3
(2 rows)

database=# select * from test1 group by a;
ERROR:  column "test1.b" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select * from test1 group by a;

PostgreSQL decides you don’t want undefined / random data in your result set and lets you know. If you’re using GROUP BY queries in MySQL, and rely on the result being, you know, correct, it may be time for a quick audit of your SQL.

A quick search of Stack Overflow questions shows many solutions which include this flaw. The solutions work fine on MySQL, but fail on PostgreSQL.

I’m surprised that 11 years after first learning of MySQL’s handling of the GROUP BY clause, it has the same behaviour enabled by default. MySQL’s behaviour can be modified to behave in a stricter manner. Worth doing if you want to ensure you receive results without including randomly selected data.

Categories: Development, Geeky, Ruby Tags:

On Daemons

August 7th, 2010 No comments

New post on Daemons over on the Booko blog.

Categories: Booko, Development, Geeky, Ruby, SysAdmin Tags:

It’s not just Books.

November 12th, 2009 No comments

If you had the idea that books were the only thing Australians over paid for (I know, none of you believe that) check this out from Nintendo. The new Super Mario Bros. has an RRP in the US of $49 USD which is $50.235 AUD. You can buy it on Amazon.  According to EBGames, the RRP for the Australian version is $98 AUD, for a markup of almost 100% of the US price.

Categories: Uncategorized Tags:

Google gets some numbers numbers from the world bank

November 12th, 2009 No comments

Via The Age

Categories: Uncategorized Tags:

Playing on the Master branch

November 2nd, 2009 No comments
Categories: Uncategorized Tags:

Fun with git post-commit

October 24th, 2009 No comments

Over on the Booko Blog.

Categories: Uncategorized Tags:

Are you an artist in search of prizes to enter?

October 4th, 2009 No comments

If so, a friend of mine is running an art prize. Check it out: http://www.therealartprize.com/

Entry is free and there’s a cash prize. Nothing to loose!

Categories: Uncategorized Tags:

On Users and Passwords

September 23rd, 2009 No comments

Just posted on the Booko Blogo an article about implementing Users and Passwords.

Categories: Booko, Development, Geeky, Ruby Tags:

Need a MacBook Pro?

August 30th, 2009 No comments

I’m selling my last one on Ebay right now!

Get yourself a bargain!

Categories: Uncategorized Tags: