国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

oracle中distinct的用法詳解

2020-07-26 14:15:04
字體:
供稿:網(wǎng)友

下面先來看看例子:

table表

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

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

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

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

----------

name    a    b    c

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

select distinct name, id from table

結(jié)果會是:

----------

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

distinct怎么沒起作用?作用是起了的,不過他同時作用了兩個字段,也就是必須得id與name都相同的才會被排除。。。。。。。

我們再改改查詢語句:

 select id, distinct name from table

很遺憾,除了錯誤信息你什么也得不到,distinct必須放在開頭。難到不能把distinct放到where條件里?能,照樣報錯。

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

下面方法也不可行:

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

結(jié)果:

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

依然報錯, 

group by 必須放在 order by 和 limit之前,不然會報錯

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

偶認(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 tselect distinct t.* from test1 t

結(jié)果:

1    zjx    001
2    zjx    002

三、單字段

select distinct t.name from test1 t

結(jié)果:

1    zjx

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 临颍县| 漯河市| 天长市| 杨浦区| 芒康县| 贵阳市| 台州市| 安新县| 奉化市| 漠河县| 垣曲县| 大同县| 弋阳县| 平谷区| 沂水县| 仙桃市| 桃江县| 嘉祥县| 巴林右旗| 宝山区| 泽普县| 鹤庆县| 株洲市| 乌拉特后旗| 南京市| 汕尾市| 班玛县| 三门县| 宁津县| 连平县| 长武县| 九江县| 阳新县| 思南县| 镇远县| 海口市| 南康市| 庄浪县| 合川市| 宁国市| 云安县|