in reply to Sort label

First a couple nits about your post (just to show how picky monks can be): you're missing a close-quote on the sql statement, as well as the close curly on the for loop. Also, the print statement in the for loop shows two values per line, separated by space, but (viewing the source html for your page, just to be sure) the output you show has only one value per line -- BTW, it's okay to put <code> tags around data as well as around code -- and it really helps, too. (In this case, the glitches are not a big deal, because the code is both brief and fairly clear in general, but sometimes monks will complain when the posted code is alleged to produce the posted output, but it doesn't even compile.)

Then there's a slight confusion about the statement of the problem: you say:

i only want the F labels if they don't exist in the G labels so in this case i would want my output to be F01213.
Does that actually mean that you don't want the output to show the G labels at all (meaning that the G labels are only being selected so that you know which F labels to discard)?

Something like gryphon's idea, using a hash, is probably the first thing I would try. But maybe I'd query for the "G" records first, to populate the hash using just the numeric part of those IDs, then query for the "F" records, and print the ones whose ID numbers do not exist in the hash. (If you want to print the G labels in combination with the distinct F labels, that's still easy enough -- just add the distinct F entries to the hash as you go, and print the whole hash afterwards.)