sql よく使うSQL PostgreSQL
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql よく使うSQL PostgreSQL相关的知识,希望对你有一定的参考价值。
create table users (
,id serial primary key,
,name varchar(255),
,score real,
,team varchar(255)
);
insert into users (name, score, team) values
,('taguch', 5.5, 'red'),
,('fkoji', 8.3, 'blue'),
,('akane', 2.2, 'blue'),
,('sasaki', 5.0, 'green'),
,('sasaki', 4.6, 'red'),
,('kimura', 4.7, 'green');
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
抽出
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
select * from users where score > 4.0;
select * from users where name = 'taguch';
select * from users where name like '%i';
select * from users where name like 'sa_aki';
select * from users order by score;
select * from users order by team, score desc;
3行目から3行を表示
select * from users limit 3 offset 3;
select count(*) from users;
select distinct team from users;
select sum(score) from users;
select max(score) from users;
select avg(score) from users;
select team, sum(score) from users group by team;
select team, sum(score) from users group by team having sum(score) > 10.0;
select name, length(name) from users;
select concat(name, ' (', team, ')') from users;
select concat(name, ' (', team, ')') as namelabel from users;
select substring(team, 1, 1) as teaminitial from users;
select * from users order by random() limit 1;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
更新と削除
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
update users set score = 5.8 where name = 'taguch';
update users set score = score + 1 where team = 'red' or team = 'green';
delete from users where score < 3.0;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
カラム追加
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
alter table users add fullname varchar(255);
alter table users drop fullname;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
カラム変更
alter table users rename name to myname;
alter table users alter myname type varchar(32);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
索引の追加と削除
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
create index team_index on users(team);
drop index team_index;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
便利コマンド
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
現在の時刻に1日を足した時刻を表示する
select current_timestamp + interval '1' day;
現在から半年前までと条件をつける
where day >= (current_timestamp - interval '6' month)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
変数をセットする
\set value 2
select :value;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
制限をかける
offset 0 limit 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
型変換
ALTER TABLE diff_price ALTER COLUMN price TYPE double precision;
alter table stock_day1 add open_price double precision, add hight_price double precision, add low_price double precision ;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
桁修正
cast((1.444453252) as dec(14,2));
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
書き出し
psql stock -c 'select day, closing_price from stock_day1 where code = 1333;' aya -A -F',' -t > /Users/aya/Downloads/1333.csv
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
volume, price 偏差値90以上を検索
select distinct p.code as over70_code,p.day as over70_day, p.price_hensati, v.code, v.day, v.volume_hensati
from price_hensati p, volume_hensati v
where p.code = v.code and p.day = v.day and p.price_hensati >= 90 and v.volume_hensati >= 90
以上是关于sql よく使うSQL PostgreSQL的主要内容,如果未能解决你的问题,请参考以下文章