当前位置:   article > 正文

postgres sql语句总结_pgsq but type character varying

pgsq but type character varying

创建表:

  1. create table tablename
  2. (field1 integer not null peimary key, //int,不为空,主键
  3. field2 character varying(10) not null, //char,10个字节,不为空
  4. field3 serial not null, //int,自增,不为空
  5. field4 text, //char,文本
  6. field5 numeric, //int,数字
  7. field6 timestamp without time zone, //不带时区的时间
  8. field7 numeric default 0) //int,数字,默认0

插入数据:

insert into tablename(field1,field2,field3) values(1,'%2',3,'%4',5,'2060-06-06','7')

查询某字段不为空(为空):

  1. //不为空
  2. SELECT * FROM "tablename" WHERE "fieldname" IS NOT NULL
  3. //为空
  4. SELECT * FROM "tablename" WHERE "fieldname" IS NULL

计算某长度字段长度:

select sum("field") from "tablename"

统计某字段个数:

select count("field") from "tablename"

查询某字段范围内数据:

select "filed" from "tablename" where "field" between '' and ''

查询某字段符合条件:

  1. select "fieldname" from "tablename" where "field" ~* '^%1$'//全匹配%1
  2. select "fieldname" from "tablename" where "field" ~* '%1'//不匹配%1
  3. select "fieldname" from "tablename" where "field" ~* '%1$'//后匹配%1
  4. select "fieldname" from "tablename" where "field" ~* '^%1'//前匹配%1
  5. //多条件查询
  6. select "fieldname" from "tablename" where "field1" ~* '%1' and "field2" ~* '%2'
  7. select "fieldname" from "tablename" where "field" ~* '%1' or "field" ~* '%2'
  8. //跨表查询
  9. select "fieldname1" from "tablename1" where "field1" in (select "fieldname2" from "tablename2" where "field2" ~* '%1')
  10. //通过多条件查询跨表统计某字段个数
  11. select count(*) from "tablename1" where "fieldname1" in (select "fieldname2" from "tablename2" where ("field1" ~* '%1' or "field1" ~* '%2') and ("filed2" between '%3' and '%4') ) or "fieldname3" in (select "fieldname4" from "tablename3" where ("filed3" ~* '%5'))

查询某字段中的数据存在且不重复

select distinct "field" from "tablename" where "field" is not null

同表拼接多字段去重查询

  1. 最终查询的字段必须在group by里出现
  2. select concat("fieldname1","fieldname2"),"fieldname1" from "tablename" group by concat("fieldname1","fieldname2")//查询拼接fieldname1,filedname2字段不重复数据
  3. select "fieldname1" from (select concat("fieldname2","fieldname3"),"fieldname1" from "tablename" group by concat("fieldname2","fieldname3"),"fieldname1") name
  4. //查询字段fieldname1,条件是满足拼接fieldname2,fieldname3后不重复的数据
  5. //name是from需求的别名
  6. 实例:查询圆柱体表的直径与高数据,每个圆柱体的唯一标识符是头尾结点,条件是圆柱体不重复
  7. select "d_s","length" from (select concat("s_point","e_point"),"d_s","length" from table
  8. group by concat("s_point","e_point"),"d_s","length") sum

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/266635
推荐阅读
相关标签
  

闽ICP备14008679号