当前位置:网站首页>[sql] usage of self connection
[sql] usage of self connection
2022-07-26 02:57:00 【wy_ hhxx】
《SQL Advanced tutorial 》 MICK 1-2 Usage of self connection
About... For creating tables for examples SQL Statements and sample code , Please refer to the following website .
http://www.ituring.com.cn/book/1813( Please click on “ Download with book ” Download relevant materials in Chinese )
---------------------------------------------------------------
explain : The tests in the following notes are based on postgresql14
Command line connection local PSQL: psql -U <username> -d <dbname> -h 127.0.0.1 -W
Catalog
Rearrangeable 、 array 、 Combine
Method 1 : Use the extremum function
Method 2 : Use non equivalent connection
Find locally inconsistent columns
Rearrangeable 、 array 、 Combine
Products Create table statement
CREATE TABLE Products
(name VARCHAR(16) PRIMARY KEY,
price INTEGER NOT NULL);
-- Rearrangeable · array · Combine
INSERT INTO Products VALUES(' Apple ', 50);
INSERT INTO Products VALUES(' a mandarin orange ', 100);
INSERT INTO Products VALUES(' Banana ', 80);
==========================================
test=> select * from Products
test-> ;
name | price
------+-------
Apple | 50
a mandarin orange | 100
Banana | 80
(3 rows)
test=>
A join to the same table is called “ Self join ”(self join)
(1) Rearrangeable
test=> SELECT P1.name AS name_1, P2.name AS name_2
test-> FROM Products P1, Products P2;
name_1 | name_2
--------+--------
Apple | Apple
Apple | a mandarin orange
Apple | Banana
a mandarin orange | Apple
a mandarin orange | a mandarin orange
a mandarin orange | Banana
Banana | Apple
Banana | a mandarin orange
Banana | Banana
(9 rows)
test=>
(2) array
Remove pairs made of the same elements , for example ( Apple , Apple )
test=> SELECT P1.name AS name_1, P2.name AS name_2
test-> FROM Products P1, Products P2
test-> WHERE P1.name <> P2.name;
name_1 | name_2
--------+--------
Apple | a mandarin orange
Apple | Banana
a mandarin orange | Apple
a mandarin orange | Banana
Banana | Apple
Banana | a mandarin orange
(6 rows)
test=>
(3) Combine
test=> SELECT P1.name AS name_1, P2.name AS name_2
test-> FROM Products P1, Products P2
test-> WHERE P1.name > P2.name;
name_1 | name_2
--------+--------
Apple | a mandarin orange
Banana | a mandarin orange
Banana | Apple
(3 rows)
test=>
Delete duplicate lines

The reconstruction Products
test=> DROP TABLE Products;
DROP TABLE
test=>
test=> CREATE TABLE Products
test-> (rowid INTEGER PRIMARY KEY,
test(> name VARCHAR(16),
test(> price INTEGER NOT NULL);
CREATE TABLE
test=>
test=> INSERT INTO Products VALUES(1, ' Apple ',50);
INSERT 0 1
test=> INSERT INTO Products VALUES(2, ' a mandarin orange ',100);
INSERT 0 1
test=> INSERT INTO Products VALUES(3, ' a mandarin orange ',100);
INSERT 0 1
test=> INSERT INTO Products VALUES(4, ' a mandarin orange ',100);
INSERT 0 1
test=> INSERT INTO Products VALUES(5, ' Banana ',80);
INSERT 0 1
test=>
test=> SELECT * FROM Products;
rowid | name | price
-------+------+-------
1 | Apple | 50
2 | a mandarin orange | 100
3 | a mandarin orange | 100
4 | a mandarin orange | 100
5 | Banana | 80
(5 rows)
test=>Use the associated subquery to delete duplicate rows
Method 1 : Use the extremum function
test=> DELETE FROM Products P1
test-> WHERE rowid < ( SELECT MAX(P2.rowid)
test(> FROM Products P2
test(> WHERE P1.name = P2. name
test(> AND P1.price = P2.price ) ;
DELETE 2
test=>
test=> SELECT * FROM Products;
rowid | name | price
-------+------+-------
1 | Apple | 50
4 | a mandarin orange | 100
5 | Banana | 80
(3 rows)
test=>
Method 2 : Use non equivalent connection
test=> DELETE FROM Products P1
test-> WHERE EXISTS ( SELECT *
test(> FROM Products P2
test(> WHERE P1.name = P2.name
test(> AND P1.price = P2.price
test(> AND P1.rowid < P2.rowid );
DELETE 2
test=> SELECT * FROM Products;
rowid | name | price
-------+------+-------
1 | Apple | 50
4 | a mandarin orange | 100
5 | Banana | 80
(3 rows)
test=>
Find locally inconsistent columns
【 example 】 Suppose there is such an address table , The primary key is the person's name , The same family ID equally

If the family ID equally , The address must also be the same , It can be said that Mr. and Mrs. Maeda are like this “ Records of the same family with different addresses ” It's a hand error , Non equivalent self connection can be used to find such records
Addresses Create table statement
-- Find locally inconsistent columns
CREATE TABLE Addresses
(name VARCHAR(32),
family_id INTEGER,
address VARCHAR(32),
PRIMARY KEY(name, family_id));
INSERT INTO Addresses VALUES(' Maeda Yiming ', '100', ' Tiger gate, Tokyo Metropolitan port area 3-2-29');
INSERT INTO Addresses VALUES(' Maeda Youmei ', '100', ' Tiger gate, Tokyo Metropolitan port area 3-2-92');
INSERT INTO Addresses VALUES(' Kato tea ', '200', ' West Shinjuku, Shinjuku District, Tokyo 2-8-1');
INSERT INTO Addresses VALUES(' Kato Sheng ', '200', ' West Shinjuku, Shinjuku District, Tokyo 2-8-1');
INSERT INTO Addresses VALUES(' Sherlock Holmes ', '300', ' Baker Street 221B');
INSERT INTO Addresses VALUES(' Watson ', '400', ' Baker Street 221B');
Find records with the same family but different addresses
test=> select * from Addresses;
name | family_id | address
----------+-----------+-------------------------
Maeda Yiming | 100 | Tiger gate, Tokyo Metropolitan port area 3-2-29
Maeda Youmei | 100 | Tiger gate, Tokyo Metropolitan port area 3-2-92
Kato tea | 200 | West Shinjuku, Shinjuku District, Tokyo 2-8-1
Kato Sheng | 200 | West Shinjuku, Shinjuku District, Tokyo 2-8-1
Sherlock Holmes | 300 | Baker Street 221B
Watson | 400 | Baker Street 221B
(6 rows)
test=>
test=> SELECT DISTINCT A1.name, A1.address
test-> FROM Addresses A1, Addresses A2
test-> WHERE A1.family_id = A2.family_id
test-> AND A1.address <> A2.address ;
name | address
----------+------------------------
Maeda Yiming | Tiger gate, Tokyo Metropolitan port area 3-2-29
Maeda Youmei | Tiger gate, Tokyo Metropolitan port area 3-2-92
(2 rows)
test=>【 example 】 Use non equivalent self connection to find Products Goods with the same price in
test=> DROP TABLE Products;
DROP TABLE
test=> CREATE TABLE Products
test-> (name VARCHAR(16) PRIMARY KEY,
test(> price INTEGER NOT NULL);
CREATE TABLE
test=> DELETE FROM Products;
DELETE 0
test=> INSERT INTO Products VALUES(' Apple ',50);
INSERT 0 1
test=> INSERT INTO Products VALUES(' a mandarin orange ',100);
INSERT 0 1
test=> INSERT INTO Products VALUES(' grapes ',50);
INSERT 0 1
test=> INSERT INTO Products VALUES(' watermelon ',80);
INSERT 0 1
test=> INSERT INTO Products VALUES(' lemon ',30);
INSERT 0 1
test=> INSERT INTO Products VALUES(' Banana ',50);
INSERT 0 1
test=>
test=> SELECT * FROM Products
test-> ;
name | price
------+-------
Apple | 50
a mandarin orange | 100
grapes | 50
watermelon | 80
lemon | 30
Banana | 50
(6 rows)
test=> SELECT DISTINCT P1.name, P1.price
test-> FROM Products P1, Products P2
test-> WHERE P1.price = P2.price
test-> AND P1.name <> P2.name;
name | price
------+-------
Banana | 50
Apple | 50
grapes | 50
(3 rows)
test=>
Sort
【 example 】 Set the table according to the price from high to low Products Sort the goods in . We make the same order of goods with the same price , And then their products have two sorting methods , One is to skip the next position , The other is not to skip the next position .
test=> SELECT name, price,
test-> RANK() OVER (ORDER BY price DESC) AS rank_1,
test-> DENSE_RANK() OVER (ORDER BY price DESC) AS rank_2
test-> FROM Products;
name | price | rank_1 | rank_2
------+-------+--------+--------
a mandarin orange | 100 | 1 | 1
watermelon | 80 | 2 | 2
Apple | 50 | 3 | 3
grapes | 50 | 3 | 3
Banana | 50 | 3 | 3
lemon | 30 | 6 | 4
(6 rows)
test=>
【 example 】 Do not rely on sorting function implementation , If the same order has appeared , Then skip the next position
test=> SELECT P1.name,
test-> P1.price,
test-> (SELECT COUNT(P2.price)
test(> FROM Products P2
test(> WHERE P2.price > P1.price) + 1 AS rank_1
test-> FROM Products P1
test-> ORDER BY rank_1;
name | price | rank_1
------+-------+--------
a mandarin orange | 100 | 1
watermelon | 80 | 2
Apple | 50 | 3
grapes | 50 | 3
Banana | 50 | 3
lemon | 30 | 6
(6 rows)
test=>
边栏推荐
- 简单使用 MySQL 索引
- 第3章业务功能开发(删除线索)
- Application of shift distance and hypothesis
- Influence of middle tap change on ZVS oscillation circuit
- AMD64(x86_64)架构abi文档:中
- MySQL建Websites数据表
- [reading notes] user portrait methodology and engineering solutions
- GAMES101复习:着色(Shading)、渲染管线
- Audio and video technology development weekly | 254
- 【方向盘】启动命令和IDEA如何传递:VM参数、命令行参数、系统参数、环境变量参数、main方法参数
猜你喜欢

Turn on the LED

Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic

HLS实验一--乘法器

Literature speed reading | in the face of danger, anxious people run faster?

规范自己debug的流程

Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!

Application of shift distance and hypothesis

Machine learning foundation plan 0-2: what is machine learning? What does it have to do with AI?

(九)属性自省

The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
随机推荐
From the annual reports of major apps, we can see that user portraits - labels know you better than you do
JS get the time composition array of two time periods
assert _ Aligns
案例:使用keepalived+Haproxy搭建Web群集
这种动态规划你见过吗——状态机动态规划之股票问题(上)
Annotation development
Anti electronic ink screen st7302
ES6 advanced - using prototype object inheritance methods
Difference between soft link and hard link
重装Win7系统如何进行?
Project management: lean management method
MySQL build websites data table
Nahamcon CTF 2022 babyrev reverse analysis
[纯理论] YOLO v4: Optimal Speed and Accuracy of Object Detection
el-table 表头合并前四列,合并成一个单元格
Image recognition (VII) | what is the pooling layer? What's the effect?
基础知识-网络与服务器
The sixth day of the third question of daily Luogu
MySQL教程:MySQL数据库学习宝典(从入门到精通)
Literature speed reading | in the face of danger, anxious people run faster?