如何选择两列,其中一列必须是DISTINCT? [英] How to SELECT two columns, where one column must be DISTINCT?
本文介绍了如何选择两列,其中一列必须是DISTINCT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Table1
具有3列:col1
,col2
,col3
我如何SELECT
col1
的所有DISTINCT
值,其中col3
等于某个值,然后按col2 DESC
对其进行排序,但具有不同的col1
结果显示其对应的col2
值吗?
How can I SELECT
all the DISTINCT
values of col1
where col3
equals a certain value, then sort it by col2 DESC
, yet have the distinct col1
results show their corresponding col2
value?
我尝试了以下操作,但没有用:
I tried the following but it did not work:
SELECT DISTINCT (col1), col2
FROM `Table1`
WHERE `col3` = 'X'
ORDER BY `col2` DESC
以上内容不会导致col1
的不同值.如果我删除", col2
",那么它将显示不同的col1
值,但不会显示它们对应的col2
值.
The above does not result in distinct values of col1
. If I remove ", col2
", then it will show distinct values of col1
, but it won't show me their corresponding col2
values.
那我该怎么做呢?
推荐答案
也许是这样的:
SELECT col1, MAX(col2) col2_max
FROM Table1
WHERE col3 = 'X'
GROUP BY col1
ORDER BY col2_max
?
您可以在此 SQL小提琴中使用它.
这篇关于如何选择两列,其中一列必须是DISTINCT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文