[英]mysql select only unique row
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
If you only need the largest id for a particular message_id 如果您只需要特定ID的最大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
Try DISTINCT keyword. 尝试使用DISTINCT关键字。 This will work definitely: 这绝对可以工作:
SELECT DISTINCT(message_id), id FROM table;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.