Rails migrations: creating a MySQL BIGINT

7/04/2010
add_column :facebook_user_id, :integer, :limit => 8 # BIGINT
No Comments

Installing the mysql gem on OS X Leopard

3/02/2009

First make sure you have the MySQL client libraries installed, then:

sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Source: http://weblog.rubyonrails.com/2007/10/26/today-is-leopard-day

No Comments

PHPMyAdmin 2.6.x bug: ISNULL

6/11/2006

The SQL parser in PHPMyAdmin 2.6.x (the version used in Plesk 7.5) falls over when confronted with an ISNULL query, for example:

SELECT ISNULL(id) FROM user

The bug is fixed in recent versions of PMA (2.9.x works fine).

No Comments

FLOAT fields in MySQL 4.1

4/04/2006

Warning: Incompatible change: As of MySQL 4.1.2, handling of the FLOAT and DOUBLE floating-point data types is more strict to follow standard SQL. For example, a data type of FLOAT(3,1) stores a maximum value of 99.9. Before 4.1.2, the server allowed larger numbers to be stored. That is, it stored a value such as 100.0 as 100.0. As of 4.1.2, the server clips 100.0 to the maximum allowable value of 99.9. If you have tables that were created before MySQL 4.1.2 and that contain floating-point data not strictly legal for the data type, you should alter the data types of those columns

from http://dev.mysql.com/doc/refman/4.1/en/upgrading-from-4-0.html

No Comments

MySQL: Using column aliases and HAVING

8/03/2006

It isn’t possible to refer to a column alias in a WHERE clause, but you can use HAVING instead.

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

This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined. For example, the following query is illegal:

SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt > 0 GROUP BY id;

The WHERE statement is executed to determine which rows should be included in the GROUP BY part, whereas HAVING is used to decide which rows from the result set should be used.

My example using HAVING:

SELECT ms.id, ms.name, COUNT( mpc.page_id ) AS count
FROM map_supplier ms
LEFT JOIN map_profile_content mpc ON ms.id = mpc.map_supplier_id
GROUP BY ms.id
HAVING count > 0
No Comments

Converting a string to an integer on the fly (for ordering) in MySQL

19/01/2006

Use CAST.

For example:

SELECT CAST(1 AS UNSIGNED);
No Comments

Replacing strings in MySQL

19/01/2006

Use REPLACE.

For example:

REPLACE( haystack , 'find' , 'replace' )
No Comments

Fetching MySQL rows that start with a number

27/10/2005

Use MySQL’s REGEXP support.

SELECT * FROM blah WHERE COLUMN REGEXP '^[0-9]+';
No Comments