如果我添加另一列,MySQL DISTINCT无法正常工作 [英] Mysql DISTINCT not working if I add another column

查看:66
本文介绍了如果我添加另一列,MySQL DISTINCT无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mysql>选择DISTINCT标题,其名称来自myadmins;

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)

编辑

我想要的不是重复标题栏

What I want is not repeting the title column

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

推荐答案

DISTINCT适用于整个数据行.由于ID在每一行上都是不同的,所以最终将得到重复的标题.

DISTINCT applies to the entire row of data. Since the ID is different on each row, then you will end up with duplicate titles.

如果需要ID,则可以使用汇总来获取MAX(ID):

If you need the ID, then you could use an aggregate to get the MAX(ID):

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

请参见带有演示的SQL提琴

这篇关于如果我添加另一列,MySQL DISTINCT无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆