I needed to see if there was already a script for the very same requirement and came up here. Since I didn't understand the pure SQL answer, I went on to create my own Python script. If somebody needs it in future, here it is,
The script will create directories based on the column name that is given as the argument. I'm not checking the argument since this is a quick and dirty script.
[wizard @ work]$ ./fetch_attach_from_bugzilla.py bug_id
[wizard @ work]$ ls
16 18 19 21 23 24 26 fetch_attach_from_bugzilla.py
[wizard @ work]$
[wizard @ work]$ cat fetch_attach_from_bugzilla.py
#!/usr/bin/python
import binascii;
import MySQLdb;
import os;
import sys;
conn = MySQLdb.connect(host='',user="",passwd="",db="bugs");
r = conn.cursor();
r.execute('select bug_id from bugs');
bug_ids = r.fetchall();
for bug_row in bug_ids:
bug_id = bug_row[0];
cmd = 'select attachments.filename,attachments.mimetype, bugs.' + sys.argv[1] + ', attach_data.thedata from attachments left join attach_data on attachments.attach_id = attach_data.id left join bugs on bugs.bug_id = attachments.bug_id where attachments.bug_id = ' + str(bug_id);
r.execute(cmd);
for adata_row in r.fetchall():
if not os.path.isdir(os.path.join(os.getcwd(),str(adata_row[2]))):
os.mkdir(str(adata_row[2]));
f = open(os.path.join(os.getcwd(),str(adata_row[2]),adata_row[0]), "wb+")
f.write(adata_row[3]);
f.close();
r.close;
conn.close;
[wizard @ work]$