DISTINCT on multiple columns
I have, SELECT DISTINCT (first),second,third FROM 开发者_StackOverflow社区table
AND i want not only the first to be DISTINCT and the second to be DISTINCT to but the third to stay without DISTINCT , i tryed like that.
SELECT DISTINCT (first,second),third FROM table
And couple more things but didnt worked.
SELECT m.first, m.second, m.third -- and possibly other columns
FROM (
SELECT DISTINCT first, second
FROM mytable
) md
JOIN mytable m
ON m.id =
(
SELECT id
FROM mytable mi
WHERE mi.first = md.first
AND mi.second = md.second
ORDER BY
mi.first, mi.second, mi.third
LIMIT 1
)
Create an index on (first, second, third)
for this to work fast.
Have you seen this post?
Select distinct from multiple fields using sql
They seem very similar, maybe you could try something like that?
Hope this helps!
精彩评论