Reputation: 783
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 |
+----+-----+------+
Upvotes: 0
Views: 90
Reputation: 3698
Try DISTINCT keyword. This will work definitely:
SELECT DISTINCT(message_id), id FROM table;
Upvotes: -1
Reputation: 30872
If you only need the largest id for a particular message_id
SELECT max(id), message_id FROM table
where to_user = ?
and deleted != ?
and del2 != ?
and is_read = '0'
group by message_id
order by id desc
Upvotes: 1
Reputation: 16524
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
Upvotes: 4