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.
I don't know why MySQL allows you to do that, but normally in SQL the following query is not valid

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

```

You shouldn't be able to specify a SELECT column if it is not

1. A `GROUP BY` column
2. An aggregate function (e.g. `MIN`, `MAX`, `COUNT`, `SUM`...)

In your case I would use this select

```
SELECT cmt_id, thread_id, cmt_text FROM comments INNER JOIN (
  SELECT MAX(cmt_id) AS cmt_id FROM comments 
  GROUP BY thread_id
) a ON a.cmt_id = comments.cmt_id 

```