如何仅从一列中选择不同的值 [英] How to select distinct value from one column only

查看:65
本文介绍了如何仅从一列中选择不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的记录如下:

key  | name
--------------
1111 | aa   
1111 | bb    
2222 | cc

key值不同时,我需要选择keyname.当我尝试时:

I need to select the key and name when the key value is distinct. When I tried:

select distinct key, name from table;

我得到了所有行,因为查询对以下列的组合采取不同的方式:keyname.但是,我需要的只是唯一的key,而我并不关心name.我有很多记录,所以我需要一种实用的方法.

I got all the rows since the query takes distinct for the combination of the columns: key and name. But, what I need is only distinct key and I don't care about the name. I have a lot of records, so I need a practical method.

推荐答案

查询:

SELECT `key`, MAX(`name`) as name
FROM `table`
GROUP BY `key`

这篇关于如何仅从一列中选择不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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