Posts Tagged ‘mysql’

Implementing an intersect in MySQL

Monday, July 19th, 2010

For those of you looking for information on the TV show “Chuck“, this is not for you. If you don’t understand what that means, then this might be for you.  Standard SQL provides a construct called union that combines the contents of two queries, which acts as a logical OR operation against two or more datasets. However, I recently found myself in the need of the similar functionality, but with an AND operation. SQL Server provides a construct called ‘intersect‘ which does just that, but it’s not ANSI SQL, and, since I was using MySQL, that didn’t help me.  I did find a way to get the data I needed, however, by using a combination of grouping and the having clause.

Here’s the problem: I had two tables of data that formed what was a many-to-many relationship (a map table was the third table). The first table contained generic data, and the second table contained free-form meta information about the records in the first table. For the purpose of illustration, imagine a set of records with a name, address and astrological sign; each one of those records could have 1 or more free-form meta fields attached to it:

My assignment was to select all records in table a that matched all of the free-form meta fields submitted from a user.  At first this seemed simple:

select * from a, b, abmap where a.id = abmap.a_id and abmap.b_id = b.id and b.meta in ('meta1', 'meta2', 'meta3');

Unfortunately, this doesn’t implement an AND; it would include records from table a that had either 1 or 2 of the requested meta values in addition to having all three.  I was looking for something like:

select all records that have meta=meta1 AND meta=meta2 AND meta=meta3

What I needed to do was to group on a field I knew had unique values in table a (using the example case, I used ‘name’), then use the having clause to count the number of rows for every name in the result set. If the number of returned rows for any given name matches the number of meta values provided by the user, the record is considered a match.

select a.* from a, b, abmap where a.id = abmap.a_id and abmap.b_id = b.id and b.meta in ('meta1','meta2','meta3') group by a.name having count(a.name) = 3;

The count function normally returns the number of rows in the query, but since the query has been grouped by the name column, count returns the number of rows in each group. Since the user had specified three different meta values, I wanted to find groups with exactly three records. By using this query, you would get all records from table a that had the values ‘meta1′, ‘meta2′ and ‘meta3′ attached to it through abmap, no more, no less.

How to fix the font in the MySQL Workbench editor

Tuesday, July 6th, 2010

Just ran into an annoying little problem using the version 5.1.12 of MySQL Workbench: the font used in the SQL editor is really small, and there’s no way to change it.  The preferences dialog shows you what the font is, but it’s a text field, and even though you can type in it, it appears to be a read-only field.

To fix it, go old school and edit the configuration file.  On Linux, it’s in your home directory in .mysql/workbench/wb_options.txt. Be sure to quit MySQL Workbench before editing the config file to avoid having your work overwritten.

Once you’ve opened the file, look for the key workbench.general.Editor:Font, and increase the font size to something usable.  I chose 11, but some may still find that too small.

Save the file, then start MySQL workbench.  You should now be able to read what you’re writing in the editor.

Issues with installing 64-bit MySQL

Monday, November 23rd, 2009

melting_mysqlIt had been a while since I needed to install MySQL (January), but a new desktop PC required a new install of MySQL.  To my delight, not only had a new version of MySQL been released, but there was a 64-bit version available as well.  Installing 32-bit code onto a 64-bit machine just seems wrong, so even though I probably don’t need the 64-bit speed for my development tasks on my PC, I went right ahead and started installing it.

Things went well up until I reached the configuration wizard. I had selected pretty much standard everything in the installer (for a development setup), but when I reached the configuration wizard, it hung just before creating the databases and configuration files. The wizard itself had to be forcibly killed.

After a bit of research, it appears this problem occurs because the wizard depends upon a 32 bit libmySQL, even though it’s installing a 64-bit package, and the 64-bit installer didn’t have the 32-bit library. Fortunately, the fix was easy: just put a 32-bit libmySQL.dll into the dll path (the %PATH% environment variable).

So where can you get a 32-bit libmySQL.dll? I already had MySQL tools installed, so I just borrowed it from there. The Windows MySQL tools are 32-bit only. You probably will need these tools, anyway, so just install them before installing the 64-bit database server.

When you start the installation, go up to the point where the “MySQL Server Instance Config Wizard” starts, and then cancel it.  Go to your 32-bit MySQL tools directory and copy the libmySQL.dll from that directory.

Next, go to your MySQL Server installation (for me, it was C:\Program Files\MySQL\MySQL Server 5.1\bin) and rename libmySQL.dll to something else, like 64libmySQL.dll, and then paste the 32-bit DLL you copied earlier.

Now go to your Start Menu, then navigate down to the MySQL Server 5.1 directory in the “All Programs” area. Inside “MySQL Server 5.1″ is the “MySQL Server Instance Config Wizard” icon.  Select that, and wait for the configuration to end.

Once the configuration has completed, make sure the databases have been stopped, and then delete the 32-bit libmySQL.dll and rename 64libmySQL.dll back to libmySQL.dll.  Start up the databases again, and you’ll experience the 64-bit MySQL goodness.