Skip to main content.
June 30th, 2008

MySQL Tip - Concatenate Multiple Values Across Rows

The function group_concat in MySQL will let you combine the values from multiple rows together in an aggregate function.

It’s a little hard to picture, so here’s a hypothetical aggregate query from the Netflix prize dataset:

select movie_id, customer_id, max(date) as latest, group_concat(rating) as all_ratings, count(*) as total_ratings
from ratings
group by movie_id,customer_id
having count(*)>1;

And we get back* **:

+----------+-------------+------------+-------------+---------------+
| MOVIE_ID | CUSTOMER_ID | LATEST     | ALL_RATINGS | TOTAL_RATINGS |
+----------+-------------+------------+-------------+---------------+
| 1        | 5           | 2007-12-02 | 3,2,4,5     | 4             |
| 54       | 3           | 2007-12-04 | 1,3,1       | 3             |
| 23       | 34          | 2007-12-07 | 5,5         | 2             |
+----------+-------------+------------+-------------+---------------+

As you can see for each set of rows grouped by the same movie_id and customer_id, it took each individual rating value and concatenated them all together in the new aggregate row.

* My actual query returned 0 rows so I thought I’d make up a quick example to illustrate the point.
** I made that pretty table*** with this ASCII table generating utility. Yay!
*** Pretty table may not be pretty in RSS readers or Blackberries.

Posted by Greg Pinero (Primary Searcher) as SQL at 9:49 PM MST

No Comments »

Mini Searches with Answers

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.

Instant SQL Formatter
Make SQL code look nice, automatically?

How to restore GRUB using the Ubuntu Live CD « Odzangba Kafui Dake’s Blog
An option for fixing grub error 18? I’ll reboot now and try it.

HTTrack is a free (GPL, libre/free software) and easy-to-use offline browser utility.
Mirror a website locally for off line access

FOSSwire » Create a mirror of a website with Wget
$ wget -mk -w 20 http://www.example.com/

mysqldump on Ubuntu - Fixing: Got error: 1: Can’t create/write to file ‘…’ (Errcode: 13) when executing ‘SELECT INTO OUTFILE’
The answer is to output to the /tmp directory. Very sucky Ubuntu!

AppArmor - Community Ubuntu Documentation
Maybe good for debugging Ubuntu permissions issues?

Tags: , , , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 5:00 AM MST

No Comments »

June 26th, 2008

Python - How to Make MySQLdb Store Query Results on the Server

Sometimes I’ll want to run a query against a MySQL database from Python that returns a large result set. So large in fact that Python hits a memory error.

One way around this is to have the Python MySQL library (MySQLdb) store the results from a query on the database server and bring them back to you one at a time as you request them.

Here’s how you set that up:

import MySQLdb
import MySQLdb.cursors #Make sure to import this seperately

#build your connection object normally but pass it a cursorclass keyword
db=MySQLdb.connect(host=HOSTNAME,user=USERNAME,passwd=PASSWORD,db=DATABASE,
            cursorclass=MySQLdb.cursors.SSCursor)

#Usage

cursor=db.cursor()
cursor.execute('select huge_column from huge_table')
#iterate over the results pulling them down from the server on each iteration
for row in cursor:
    print row
cursor.close()

Posted by Greg Pinero (Primary Searcher) as Python at 8:49 PM MST

1 Comment »

June 6th, 2008

Mini Searches with Answers

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but still super useful.

DLC Taxi/Cab Phone number for transport to Westchester (HPN) Airport
Taxi: DLC Car Service (tel: (914) 946 6664) provides taxi service from the airport.

Repairing this weird Mysql error: Table ‘./mysql/user’ is marked as crashed and should be repaired.
Repairing this weird Mysql error: Table ‘./mysql/user’ is marked as crashed and should be repaired.

geocoder.us: a free US address geocoder
Lots of geographic tools with an API too. Get lat/long of an address, find distances, etc.

Flexigrid
Lightweight but rich data grid with resizable columns and a scrolling data to match the headers, plus an ability to connect to an xml based data source using Ajax to load the content. Supposedly it can convert existing tables too?

Musings of an Anonymous Geek » Blog Archive » A Couple of MySQL Performance Tips
Useful MySQL performance tips

Add new Path variables in Ubuntu
$ sudo nano /etc/profile
Add "PATH=$PATH:/new/path\nexport PATH" to end of file and save.
Then do
$ . /etc/profile (to restart)

MySQL :: MySQL 5.0 Reference Manual :: 12.2.7.2 Index Hint Syntax
Here’s how to suggest and/or force MySQL to use an index.

How do I get my Wikipedia article to show up in the Google Earth Geographic Web layer?
You must geotag the article. The simplest way to do this is to use the {{coord}} template. This template can be used anywhere within the article text. For example, if the article for San Francisco, California contained this markup anywhere within the arti

HOWTO: open a file in Nautilus with gedit as root - Ubuntu Forums
Let’s you right click on a file and open it in a text editor as root in Ubuntu Hardy Heron (8.04)

Speeding up GROUP BY if you want aproximate results | MySQL Performance Blog
This guy claims grouping by crc32(<some string>) instead of just <some string> will make your query 120 times faster…

Places to store your kayak - The Stamford Kayaking Meetup Group (Stamford, CT) - Meetup.com
Places to store your Kayak near Stamford, CT

Thad’s Tech Blog: How to add disk space with LVM on Linux
$ sudo fdisk /dev/sda (new partition type 8e, w for write)
$ sudo pvcreate /dev/sda1
$ sudo vgdisplay
$ sudo vgextend PFA-CT-LPDB01 /dev/sda1
$ sudo lvextend -L +930G /dev/PFA-CT-LPDB01/root
$ sudo ext2online /dev/PFA-CT-LPDB

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 11:10 PM MST

Comments Off