SQL學習筆記 《SQL高階教程》1 2

2021-10-17 05:19:59 字數 4950 閱讀 1384

自連線+非等值連線

自連線+group by = 遞迴集合

表是行的集合,面向集合

開銷較大

唯二重要的方法

case 自連線

sql語言 : 面向集合的特性

有序對<>、無序對{}

--獲取可重排列(交叉連線 笛卡爾積) 3*3

select p1.name as name_1, p2.name as name_2

from products p1, product p2;

--獲取排列 a32 = 3*2

select p1.name as name_1, p2.name as name_2

from products p1, products p2

where p1.name <> p2.name;

--獲取組合c32 = 2*3 / 2

select p1.name as name_1, p2.name as name_2

from products p1, products p2

where p1.name > p2.name;

--獲取組合: 擴充套件程3列

select p1.name as name_1, p2.name as name_2, p3.name as name_3

from products p1, products p2, products as p3

where p1.name > p2.name

and p2.name > p3.name;

可用的行id僅有oracle和postgresql可以使用、且pgsl需要在建表是指定with oids

--關聯子查詢:對兩個擁有相同資料的集和進行的關聯操作

delete

from products p1

where rowid <

(select

max(p2.rowid)

)from products p2

where p1.name = p2.name

and p1.price = p2.price )

;--集合是sql唯一可以處理的資料結構

--刪除重複行(2):非等值連線 **更常用**

delete

from products p1

where

exists

(select

*from products p2

where p1.name = p2.name

and p1.price = p2.price

and p1.rowid < p2.rowid )

;

--查詢同一家人住址不同

--尋找**相等的商品組合(+ distinct 避免出現重複行)

--自連線 + 非等值連線

select

distinct a1.name, a1.address

from addresses a1, addresses a2

where a1.family_id = a2.family_id

and a1.address <> a2.address ;

-- 用於查詢**相等但商品名稱不同的記錄的 sql 語句(distinct)

select

distinct p1.name, p1.price

from products p1, products p2

where p1.price = p2.price

and p1.name <> p2.name;

--關聯子查詢方法(作業)

select p1.name, p1.price

from products as p1

where p1.name <>

(select p2.name

from products as p2

where p1.price = p2.price)

跳過之後的位次

不跳過之後的位次

--視窗函式

select name, price,

--跳過之後的位次,123336

rank(

)over

(order

by price desc

)as rank_1

--不跳過之後的位次,123334

dense_rank(

)over

(order

by price desc

)as rank_2

from products

--非等值自連線

--有幾個比自己大的再加一就是自己的排序

--排序從 1 開始。如果已出現相同位次,則跳過之後的位次

--去掉 +1 則從0排序;count(distinct p2.price),則不跳過之後的位次。

select p1.name, p1.price,

(select

count

(p2.price)

from products p2

where p2.price > p1.price)+1

as rank_1

from products p1

order

by rank_1;

--排序:使用自連線

select p1.name,

max(p1.price)

as price,

count

(p2.name)+1

as rank_1

from products p1 left

outer

join products p2

on p1.price < p2.price

group

by p1.name

order

by rank_1;

--排序:使用內連線,結果沒有第一名

select p1.name,

max(p1.price)

as price,

count

(p2.name)+1

as rank_1

from products p1 inner

join products p2

on p1.price < p2.price

group

by p1.name

order

by rank_1;

select p1.name as name_1, p2.name as name_2

from products p1 products p2

where p1.name >= p2.name;

select district, name, price,

rank over

(partition

by district order

by price desc

)as rank_1

from districtproducts;

--關聯子查詢

select p1.district, p1.name,

p1.price,

(select

count

(p2.price)

from districtproducts p2

where p1.district = p2.district /* 在同乙個地區內進行比較 */

and p2.price > p1.price)+1

as rank_1

from districtproducts p1;

/* 練習題1-2-2:自連線 */

select p1.district, p1.name,

max(p1.price)

as price,

count

(p2.name)+1

as rank_1

from districtproducts p1 left

outer

join districtproducts p2

on p1.district = p2.district

and p1.price < p2.price

group

by p1.district, p1.name;

/* 練習題1-2-3:更新位次 */

--關聯子查詢

update districtproducts2 p1

set ranking =

(select

count

(p2.price)+1

from districtproducts2 p2

where p1.district = p2.district

and p2.price > p1.price)

;

--自連線

update districtproducts2

set ranking =

(select p1.ranking

from

(select district , name ,

rank(

)over

(partition

by district

order

by price desc

)as ranking

from districtproducts2) p1

where p1.district = districtproducts2.district

and p1.name = districtproducts2.name)

;

SQL 高階教程

注 本篇部落格與w3cschool 搭配,目的是在自己學習sql時,進行總結。1.sql top select top 3 from persons 從persons表中選前三個 select 50 percent from persons 從表中選擇前一半 2.like not like sele...

SQL 高階教程概述

sql是用於訪問和處理資料庫的標準計算機語言。它是結構化查詢語言,全稱是structured query language,可讓我們訪問和處理資料庫,是一種ansi標準的計算機語言。sql union操作符合併兩個或多個select語句的結果,union內部的每個select語句必須擁有相同數量的列...

SQL學習筆記1

select語句用於從資料庫中選取資料。結果被儲存在乙個結果表中,稱為結果集。select column name,column name from table name select from table name select distinct column name,column name f...