Question: Hey! i want to know the best solution for my problem.
I have a signature generator <http://www.anitard.org/siggen/siggen_stripes/> where people can upload their own images for the signature. The problem is that my storage will get full pretty fast if i dont somehow have a script that deletes the images when they are done with the signature.

What is the best solution for this?

Answer: 
You have to have something in the lines of:

```
SELECT 
   cmt_id, 
   thread_id, 
   cmt_text 
FROM comments 
WHERE cmt_id IN (
         SELECT 
            MIN(cmt_id) 
         FROM comments 
         GROUP BY thread_id);

```

This is how SQL works, in your query it merges the rows leaving not the row with min cmt\_id but random one (or the last one but I wouldn't count on it). In the above query it would first find all the min ids and then get extended information for each one of them.
This may be just a typo, but the SQL query you have entered actually has MIN(cmt\_id) not MAX(cmt\_id) in it.

```
SELECT cmt_id, thread_id, cmt_text, MIN(cmt_id) FROM comments 
GROUP BY thread_id;

```

I think you may need to put a [rubber duck](http://www.c2.com/cgi/wiki?RubberDucking) by your monitor and talk to it

If you just want the max comment id then

```
SELECT MAX(cmt_id), thread_id FROM comments GROUP BY thread_id

```

will suffice. If you need the additional field you'll need to use a subquery as per other answers here.