PostgreSQL常用語句

2021-07-30 11:13:01 字數 1061 閱讀 7241

1、資料表及csv檔案的匯入匯出(資料表已經建好):

(1)將t1匯出位csv檔案src.csv(帶列名):

copy t1 to '檔案位置\\src.csv' with csv header;
(2)將src.csv匯入資料庫的t2表中:

copy t2 from '檔案位置\\src.csv' with csv header;

2、從表t2中查詢一些字段插入到表t1中:

insert into 表1(欄位1, 欄位2, 欄位3) select 欄位1, 欄位2, 欄位3 from t2;

3、根據表t1中的字段1在表t2中查詢對應的字段2,如根據表t1中的id在表t2中查詢學號為id的學生的age。

select age from t2 where id in (select id from t1);

4、根據t1和t2中的公共欄位id將表t2中的字段1和字段2更新到表t1:

update t1

set 欄位1 = (select t2.欄位1 from t2 where t1.id = t2.id),

欄位2 = (select t2.欄位2 from t2 where t1.id = t2.id);

注意:若t2中有相同的id,則在執行時會出錯。

5、統計表table中不同的(欄位1,欄位2)及數量(不考慮其他字段是否相同):

select distinct (欄位1, 欄位2), count(*) from table group by (欄位1, 欄位2);
6、根據id分組,最多顯示表table中每個分組的x條記錄。

select * from (

select *, row_number() over(partition by id) as row from table) t

where row <= x;

mysql常用語句 MySQL常用語句

create table student id int primary key auto increment comment 學號 name varchar 200 comment 姓名 age int comment 年齡 comment 學生資訊 修改表注釋 alter table studen...

php mysql 常用語句 mysql常用語句

一 修改mysql使用者密碼 mysql h localhost u root p 命令列登入 update user set password password 123456 where user root 二 資料庫操作 show databases 顯示資料庫 create database ...

sql常用語句

use myoa select from delete from department where departmentid 1 insert department departmentid,departmentname values 1,技術部 update department set depa...