1.distinct單列
select distinct(a) from tableA;
2.distinct多列
select distinct a,b,c from tableA;
注意此時是將a,b,c三列所有不同的組合全部列出來,而不僅僅只是distinct a
相當於以下語句:
select a,b,c from tableA group by a,b,c
3.另外一種的distinct多列
其實這篇文章的最初需求如下:
想分別查某一張表的幾個字段的distinct值
select distinct a from tableA; select distinct b from tableA; select distinct c from tableA;
這樣是可以達到目的的。但是這樣要寫三條語句,不爽,想着用一條語句達到目的。
思考了一會,想到用union來解決這個問題。
select distinct(a) || ' a' from tableA union all select distinct(b) || ' b' from tableA union all select distinct(c) || ' c' from tableA
這樣就達到了一條語句查詢出所有結果的目的。后面拼接的字符串是為了標識這個值屬於哪個字段。