将唯一关键字限制为一列 [英] limit distinct keyword to one column

查看:70
本文介绍了将唯一关键字限制为一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在mysql中应用distinct关键字,以便它仅检查一个列字段是否唯一,同时仍从我的表中返回其他列?

how do i apply the the distinct keyword in mysql so that it is only checking that one column field is unique, while still returning other columns from my table?

推荐答案

为了能够做到这一点,mysql必须知道如何处理其他列.您GROUP BY列应该是唯一的,并使用一个函数来告诉它如何处理其他列(所谓的

For being able to do that, mysql must know what to do with the other columns. You GROUP BY the column that should be unique and use a function that will tell it what to do with the others (a so-called aggregate function). MAX() and COUNT() are common examples:

SELECT studentId, COUNT(courseId) AS AmountEnrolledCourses
FROM student_enrollment
GROUP BY studentId

SELECT athlete, MAX(distance) AS PersonalRecord
FROM longjump
GROUP BY athlete

这篇关于将唯一关键字限制为一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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