distinct去重函數(shù)怎么在Postgresql中使用-創(chuàng)新互聯(lián)

distinct去重函數(shù)怎么在Postgresql中使用?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)堅(jiān)信:善待客戶,將會(huì)成為終身客戶。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨嚒N覀儚牟缓鲇瞥踉L客戶,我們用心做好本職工作,不忘初心,方得始終。十年網(wǎng)站建設(shè)經(jīng)驗(yàn)創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營(yíng)銷服務(wù)商,為您提供成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、H5場(chǎng)景定制、網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、微信小程序開發(fā)服務(wù),給眾多知名企業(yè)提供過(guò)好品質(zhì)的建站服務(wù)。

distinct 實(shí)例:


1、創(chuàng)建表:user

CREATE TABLE `user` (
 `name` varchar(30) DEFAULT NULL,
 `age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('張三', 20);
INSERT INTO `user` VALUES ('李四', 22);
INSERT INTO `user` VALUES ('李四', 20);
INSERT INTO `user` VALUES ('張三', 22);
INSERT INTO `user` VALUES ('張三', 20);

查詢結(jié)果:

SELECT * FROM user
張三 20
李四 22
李四 20
張三 22
張三 20

2、根據(jù) name 查詢?nèi)ブ睾蟮臄?shù)據(jù):

SELECT distinct name FROM user
張三
李四

3、根據(jù)name 和 age 查詢?nèi)ブ睾蟮臄?shù)據(jù):

SELECT distinct name,age FROM user
張三 20
李四 22
李四 20
張三 22

4、根據(jù)name,age查詢重復(fù)數(shù)據(jù)數(shù):

SELECT distinct name,age,count(*) 數(shù)據(jù)條數(shù) FROM user GROUP BY name,age
張三 20 2
張三 22 1
李四 20 1
李四 22 1

二、查出重復(fù)數(shù)據(jù)后,我們需要?jiǎng)h除重復(fù)數(shù)據(jù)

刪除重復(fù)數(shù)據(jù)一般幾種方式,一般采用 臨時(shí)表 或者根據(jù) 某個(gè)字段,例如id等,通過(guò)max或者min函數(shù)去重。

??補(bǔ)充:基于postgresql ctid實(shí)現(xiàn)數(shù)據(jù)的差異同步

項(xiàng)目背景:

最近在做異構(gòu)數(shù)據(jù)同步方面(非實(shí)時(shí))的工作,從oracle,gbase,postgresql向mysql數(shù)據(jù)庫(kù)中同步,對(duì)于沒有自增字段(自增ID或時(shí)間字段)的業(yè)務(wù)表,做差異同步是一件非常麻煩的事情,主要體現(xiàn)在記錄的新增、更新與刪除上

備注:源庫(kù)只提供一個(gè)只讀權(quán)限的用戶

ctid在pg中的作用

ctid是用來(lái)指向自身或新元組的元組標(biāo)識(shí)符,怎么理解呢?下面能過(guò)幾個(gè)實(shí)驗(yàn)來(lái)測(cè)試一下

satdb=# create table test_ctid(id int,name varchar(100));
satdb=# insert into test_ctid values(1,‘a(chǎn)'),(1,‘a(chǎn)');
satdb=# insert into test_ctid values(2,‘a(chǎn)'),(3,‘a(chǎn)');

查看記錄的ctid值

satdb=# select id,name,ctid from test_ctid;
id | name | ctid
----±-----±------
1 | a | (0,1)
1 | a | (0,2)
2 | a | (0,3)
3 | a | (0,4)
(4 rows)

對(duì)id為2的記錄進(jìn)行更新

satdb=# update test_ctid set name=‘b' where id=2;
UPDATE 1

這里可以看到id=2的記錄指向了新的元組標(biāo)識(shí)符 (0,5)

satdb=# select id,name,ctid from test_ctid;
id | name | ctid
----±-----±------
1 | a | (0,1)
1 | a | (0,2)
3 | a | (0,4)
2 | b | (0,5)
(4 rows)
satdb=# select * from test_ctid where ctid='(0,1)';
id | name
----±-----
1 | a
(1 row)

刪除 id=3的記錄后,對(duì)應(yīng)的ctid(0,4)不存在了

satdb=# delete from test_ctid where id=3;
DELETE 1
satdb=# select *,ctid from test_ctid;
id | name | ctid
----±-----±------
1 | a | (0,1)
1 | a | (0,2)
2 | b | (0,5)
(3 rows)

再插入一條記錄時(shí),看看會(huì)不會(huì)使用(0,4)這個(gè)標(biāo)識(shí)符

satdb=# insert into test_ctid values(3,‘d');
INSERT 0 1
satdb=# select *,ctid from test_ctid;
id | name | ctid
----±-----±------
1 | a | (0,1)
1 | a | (0,2)
2 | b | (0,5)
3 | d | (0,6)

這里新插入的記錄不會(huì)使用(0,4),而是直接分配新的標(biāo)識(shí)符(0,6)

總結(jié):

1、ctid的作用與oracle rowid類似,可以標(biāo)識(shí)一條記錄

2、記錄的更新后,后生產(chǎn)新的ctid

3、記錄刪除后,新插入的記錄不會(huì)使用已經(jīng)刪除記錄的ctid

4、基于ctid可以實(shí)現(xiàn)記錄的去重操作

5、基于ctid可以實(shí)現(xiàn)差異增量同步(新增、刪除、更新)

關(guān)于distinct去重函數(shù)怎么在Postgresql中使用問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

網(wǎng)頁(yè)標(biāo)題:distinct去重函數(shù)怎么在Postgresql中使用-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)網(wǎng)址:http://muchs.cn/article36/djggsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣商城網(wǎng)站、微信小程序用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)