指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

postgressql的基本使用:

简单的几个使用sql语句:
select id,username from "user"; // 这个 user 表 一定要用 双引号引起来,不然查询报错

select * from "dict" left join "dict_type" on "dict".dict_type_id = "dict_type".id where "dict_type".id = 1;

update "dict" set value = 2 where id = 1;

// postgres 和mysql 一样 都有 distinct 函数,但是这个函数只能查所有select的字段同时不重复,为此 postgres
// 有一个distinct on 实现单个字段去重,如果加了 order by  那么第一个order by的字段 一定是 on 里面的字段,不然报错
select distinct on(dict_type_id)id, name, dict_type_id from "dict" order by dict_type_id desc, id desc;

关于 distinct on的官方介绍:
用法:
  DISTINCT ON ( expression [, …] ) keeps only the first row of each set of rows where the given expressions evaluate to equal. […]。 Note that the “first row” of each set is unpredictable unless ORDER BY is used to ensure that the desired row appears first. […]。 The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s)。
  意思是DISTINCT ON ( expression [, …] )把记录根据[, …]的值进行分组,分组之后仅返回每一组的第一行。需要注意的是,如果你不指定ORDER BY子句,返回的第一条的不确定的。如果你使用了ORDER BY 子句,那么[, …]里面的值必须靠近ORDER BY子句的最左边。