标签:数据库 指定 进入 data and postgre sql命令 统计 多列
1.连接数据库2.创建数据库
postgres=# create database testdb;
3.查看数据库
postgres=# \l
4.删除数据库
postgres=# drop database testdb;
5.进入数据库
postgres=# \c testdb;
6.列出当前库所有表
testdb=# \dt
7.创建表
testdb=# create table account(
testdb(# user_id serial primary key,
testdb(# username varchar(50) unique not null,
testdb(# password varchar(50) not null);
create table products (
testdb(# product_no integer,
testdb(# name varchar(20),
testdb(# price numeric);
create table account (
username varchar(50) unique not null,
count integer);
8.删除表
testdb=# drop table account;
9.创建模式
testdb=# CREATE SCHEMA myschema;
10.指定模式创建表
create table myschema.mytable(
user_id serial primary key,
username varchar(50) unique not null,
password varchar(50) not null);
11.删除模式里的对象
drop schema myschema CASCADE;
12.删除模式
drop schema myschema
13.插入数据
testdb=# insert into products values (1, ‘chiness‘, 9.99);
14.查询数据
testdb=# select * from products;
15.更新数据
testdb=# update products set name = ‘hyh‘ where product_no = 1;
16.删除表数据
testdb=# delete from products where name = ‘hyh‘; 字符串必须单引号
17.order by 升序,降序排列
testdb=# select from products order by price asc;
testdb=# select from products order by price desc;
18.order by 多列排序
select * from products order by price,product_no asc;
19.group by分组
testdb=# select name, sum(price) from products group by name;
按名字分组统计每个名字下的价格总额
20.HAVING子句与GROUP BY子句组合使用,用于选择函数结果满足某些条件的特定行
testdb=# select name from products group by name having name != ‘hyh‘;
21.条件查询
AND 条件
testdb=# select * from products where name = ‘hyh‘ and price = 10;
OR 条件
testdb=# select * from products where name = ‘hyh‘ or price = 10;
AND & OR 条件
testdb=# select * from products where name = ‘hyh‘ and price = 10 or product_no = 2;
NOT 条件
testdb=# select from products where name is not null;
testdb=# select from products where name not in (‘hyh‘,‘chiness‘);
LIKE 条件
testdb=# select * from products where name like ‘%ch%‘;
IN 条件
testdb=# select * from products where price in (13,15);
NOT IN 条件
testdb=# select * from products where name not in (‘hyh‘,‘chiness‘);
BETWEEN 条件
testdb=# select * from products where price between 10 and 15;
22.连接
内连接(INNER JOIN)
testdb=# select products.name,account.count from products inner join account on products.name = account.username;
左外连接(LEFT OUTER JOIN)
左外连接返回从“ON”条件中指定的左侧表中的所有行,只返回满足条件的另一个表中的行
testdb=# select products.name,account.username,account.count from products left outer join account on products.price = account.count;
右外连接(RIGHT OUTER JOIN)全连接(FULL OUTER JOIN)跨连接(CROSS JOIN)
标签:数据库 指定 进入 data and postgre sql命令 统计 多列
原文地址:https://blog.51cto.com/haoyonghui/2376807