欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

oracle中distinct的用法詳解

 更新時(shí)間:2015年09月16日 11:37:13   作者:Eric.Yan  
distinct這個(gè)關(guān)鍵字來(lái)過(guò)濾掉多余的重復(fù)記錄只保留一條,但往往只用它來(lái)返回不重復(fù)記錄的條數(shù),而不是用它來(lái)返回不重記錄的所有值。其原因是distinct只有用二重循環(huán)查詢來(lái)解決,而這樣對(duì)于一個(gè)數(shù)據(jù)量非常大的站來(lái)說(shuō),無(wú)疑是會(huì)直接影響到效率的。

下面先來(lái)看看例子:

table表

字段1     字段2    id        name    1           a    2           b    3           c    4           c    5           b

庫(kù)結(jié)構(gòu)大概這樣,這只是一個(gè)簡(jiǎn)單的例子,實(shí)際情況會(huì)復(fù)雜得多。

比如我想用一條語(yǔ)句查詢得到name不重復(fù)的所有數(shù)據(jù),那就必須使用distinct去掉多余的重復(fù)記錄。

select distinct name from table 得到的結(jié)果是:

----------

name    a    b    c

好像達(dá)到效果了,可是,我想要得到的是id值呢?改一下查詢語(yǔ)句吧:

select distinct name, id from table

結(jié)果會(huì)是:

----------

id name    1 a    2 b    3 c    4 c    5 b

distinct怎么沒(méi)起作用?作用是起了的,不過(guò)他同時(shí)作用了兩個(gè)字段,也就是必須得id與name都相同的才會(huì)被排除。。。。。。。

我們?cè)俑母牟樵冋Z(yǔ)句:

 select id, distinct name from table

很遺憾,除了錯(cuò)誤信息你什么也得不到,distinct必須放在開(kāi)頭。難到不能把distinct放到where條件里?能,照樣報(bào)錯(cuò)。

------------------------------------------------------------------------------------------------------------

下面方法也不可行:

select *, count(distinct name) from table group by name

結(jié)果:

ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"

依然報(bào)錯(cuò), 

group by 必須放在 order by 和 limit之前,不然會(huì)報(bào)錯(cuò)

------------------------------------------------------------------------------------------------------------

偶認(rèn)為這樣可行

select max(id), name from table group by name;

結(jié)果:

id name
1  a
2  b
4  c
5  d

用法二:

一、數(shù)據(jù):

1    zjx    001    AAAiBZAAQAAAAVPAAA
2    zjx    002    AAAiBZAAQAAAAVPAAB
3    zjx    001    AAAiBZAAQAAAAVPAAC

二、多字段

select distinct t.name,t.code from test1 t
select distinct t.* from test1 t

結(jié)果:

1    zjx    001
2    zjx    002

三、單字段

select distinct t.name from test1 t

結(jié)果:

1    zjx

相關(guān)文章

最新評(píng)論