-3

mysql> select DISTINCT title, id from myadmins;

+------+------------+
| id   | title      |
+------+------------+
|    1 | admin      |
|    2 | stack      |
|    3 | jeff       |
|    4 | admin      |
|    5 | stack      |
+------+------------+
1 row in set (0.00 sec)

编辑

我想要的不是重复标题栏

+------+------------+
| id   | title      |
+------+------------+
|    2 | stack      |
|    3 | jeff       |
|    4 | admin      |
+------+------------+
1 row in set (0.00 sec)
4

2 回答 2

4

DISTINCT适用于整行数据。由于ID每一行都不同,因此您最终会得到重复的标题。

如果您需要ID,那么您可以使用聚合来获取MAX(ID)

select max(id) id,
  title
from yourtable
group by title
order by id

请参阅带有演示的 SQL Fiddle

于 2013-01-20T23:01:54.780 回答
2

你会得到不同的(id,title)情侣。

行与id=1 and title=admin行不同id=4 and title=admin

如果您只想从表中获得不同的标题:

select DISTINCT title from myadmins;

+------------+
| title      |
+------------+
| admin      |
| stack      |
| jeff       |
+------------+
于 2013-01-20T22:41:10.847 回答