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?
SELECT DISTINCT Column_1, Column_2, Column_3 FROM Table
You may, however, want to elaborate on your question as it seems a little too straight forward for my answer to be THE answer.
When you lead the column name with DISTINCT it's only checking on that column. Am I misunderstanding the question?
You'll need to use group by instead.
SELECT SUM(A), MIN(B), C FROM TABLE WHERE A > 1 GROUP BY C;
Note that if you're grouping on one column and returning others you have to provide some sort of way to cram all those values in the other fields into one. That way is called an aggregate function, and you'll have to check the manual for your database version to know exactly what your options are. See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html for mysql.
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