在多列上区分 [英] DISTINCT on multiple columns
本文介绍了在多列上区分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有,SELECT DISTINCT (first),second,third FROM table
而且我不仅希望第一个成为DISTINCT,第二个希望成为DISTINCT,而且我希望第三个保持不区分DISTINCT,
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
)
在(first, second, third)
上创建索引以使其快速运行.
Create an index on (first, second, third)
for this to work fast.
这篇关于在多列上区分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文