Bay Cao và Bay Xa – Fly High and Fly Far

December 22, 2008

How to select distinct with multi field

Filed under: MySQL — doqkhanh @ 1:13 AM

In the most common usage, select distinct using as select operator for only 1 field, for example:

SELECT DISTINCT memid
FROM common.members
WHERE memid > 3000;

But sometime, you need select associated field like username or password, or point, you need a query like this:

SELECT DISTINCT (memid), MAX(point2) AS point2max
FROM common.mempoint200812
WHERE clientid = 27
AND pointkind = 20
GROUP BY memid
ORDER BY point2max DESC ;

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.