distinct sql用法_PG语法解剖--基本sql语句用法入门

概述

今天主要对PG数据库的一些基本SQL语句用法做个介绍,做个简单了解,也做备忘!

下面主要用例子来说明。

13a5067f91631a33e378f892b2f3b3ba.png

1、建表语句

create table test (id int8 primary key,info text,crt_time timestamp);

注意保留字


2、select into & create table as

postgres=# select * into table new_tbl from pg_class;postgres=# create table tbl_1 as select * from pg_class;
c1ac33e884ca3ca7b04147b906287818.png

3、插入更新删除查询

insert into tbl (xx,xx) values (xx,xx);update tbl set xx=xx where xxx;delete from tbl where xxx=xxx;select xx from xx where xx...;

如果是delete|update limit,则:

update tbl set xx=xx where ctid = any ( array (select ctid from tbl where xx limit ? for update));delete from tbl where ctid = any ( array (select ctid from tbl where xx limit ? for update));

4、批量DML

insert into xx values (),(),...(); copy xx from stdin; copy xx from 'file'; pg_bulkloadupdate t set info=t1.info,crt_time=t1.crt_time from t1,t2 where (t.id=t1.id) and t1.id=t2.id;update tbl_1 set relname=tmp.rel from (values (1,'test1'),(2,'test2')) tmp (id, rel) where tmp.id=tbl_1.id;delete from t using t1 where t.id=t1.id;delete from tbl_1 using (values (1),(2)) tmp (rel) where tmp.rel=tbl_1.reltype;

注意update , delete 批量操作,JOIN不是一一对应时,更新目标可能会随机匹配。


5、DB端copy+客户端copy

• https://github.com/digoal/blog/blob/master/201805/20180516_03.md

• https://github.com/digoal/blog/blob/master/201805/20180510_01.md

5.1、copy为什么快?

协议:

ac23ae34acf3c5d588dd6698bacfd22d.png

5.2、DB 端copy

copy tbl to 'file';copy (SQL) to 'file';copy tbl from 'file';
ed2e89aee543098c68c10820cb73064f.png

5.3、客户端copy

copy tbl from stdin;copy (SQL) to stdout;copy tbl to stdout;psql (copy to | from); -- copy协议

6、排序+ offset limit

select * from tbl_1 order by relname nulls first;select * from tbl_1 order by relname nulls last;select * from tbl_1 order by relname;select * from tbl_1 order by relname limit 10 offset 10;select * from tbl_1 order by relname::text collate "C";

7、聚合+解耦合

select string_agg(relname,',' order by xx) from tbl_1; select g,avg(c1) from tbl group by g;
9ab33c74ea7f1a8b08d55cb7f3d9d1b1.png

8、distinct

select distinct relname,relnamespace from pg_class;SELECT id, COUNT_DISTINCT(val) FROM test_table GROUP BY 1;select count(distinct (relname,relnamespace)) from pg_class;select distinct on (c3) c2,c3 from tbl;

9、INNER|OUTER JOIN

•inner

select * from t1 join t2 on (t1.x=t2.x) where xxxx;

• left

1)scan filter

select t1.*,t2.* from t1 left join t2 on (t1.x=t2.x) where t1.x=x;

2)join filter

select t1.*,t2.* from t1 left join t2 on (t1.x=t2.x and t1.x=x);

• right

把上面的left join改成right join即可,这里就不多说了。


篇幅有限,这块内容就介绍到这了,后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注一下~

f501d2f2884696c8e212dfd03295e649.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的distinct用法SQL中的distinct用法有些不同,下面分别介绍一下。 ## Java中distinct用法 在Java中,distinct主要用于去除集合中的重复元素,常用于List和Stream集合。 ### List集合中的distinct用法 ```java List<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("orange"); list.add("banana"); list.add("pear"); List<String> distinctList = list.stream().distinct().collect(Collectors.toList()); System.out.println(distinctList); ``` 输出结果为: ``` [apple, banana, orange, pear] ``` ### Stream集合中的distinct用法 ```java Stream<String> stream = Stream.of("apple", "banana", "orange", "banana", "pear"); List<String> distinctList = stream.distinct().collect(Collectors.toList()); System.out.println(distinctList); ``` 输出结果为: ``` [apple, banana, orange, pear] ``` ## SQLdistinct用法SQL中,distinct主要用于去除查询结果中的重复行。 ### 示例1:查询表中的不同城市 ```sql SELECT DISTINCT city FROM employee; ``` ### 示例2:查询表中的不同部门和职位 ```sql SELECT DISTINCT department, position FROM employee; ``` ### 示例3:查询表中的不同城市和部门 ```sql SELECT DISTINCT city, department FROM employee; ``` ### 示例4:根据多个字段去重查询 ```sql SELECT DISTINCT ON (city, department) city, department, position FROM employee; ``` 以上四个示例中,第一个和第二个示例比较简单,第三个示例中查询的是两个字段的组合,第四个示例中使用了DISTINCT ON关键字,可以根据多个字段去重,但是只会保留第一个符合条件的记录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值