国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 桃源县| 龙陵县| 金乡县| 赤峰市| 连江县| 浪卡子县| 阿拉善左旗| 满城县| 琼海市| 自贡市| 英吉沙县| 盐亭县| 二连浩特市| 苍南县| 双鸭山市| 崇文区| 翼城县| 开封县| 松滋市| 永胜县| 清远市| 柘城县| 子洲县| 维西| 碌曲县| 观塘区| 通许县| 西藏| 抚州市| 峨边| 郓城县| 花莲县| 常山县| 灵山县| 鲜城| 聂拉木县| 通州市| 申扎县| 双牌县| 阜南县| 图片|