mysql selects only the single line

advertisements

I would like to select only unique row from the table, can someone help me out?

 SELECT * FROM table
 where to_user = ?
 and deleted != ?
 and del2 != ?
 and is_read = '0'
  order by id desc

+----+-----+------+
| id | message_id |
+----+-----+------+
|  1 | 23         |
|  2 | 23         |
|  3 | 23         |
|  4 | 24         |
|  5 | 25         |
+----+-----+------+

I need something like

+----+-----+------+
| id | message_id |
+----+-----+------+
|  3 | 23         |
|  4 | 24         |
|  5 | 25         |
+----+-----+------+


Try this:

SELECT MAX(id), message_id
FROM tablename
GROUP BY message_id

and if you have other fields then:

SELECT MAX(id), message_id
FROM tablename
WHERE to_user = ?
AND deleted != ?
AND del2 != ?
AND is_read = '0'
GROUP BY message_id
ORDER BY id DESC