- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - Self Join
您是否认为需要比较同一表格的两栏? 例如,让我们相信,一个组织正在根据一些肤色选择一个秘密的圣塔。 这样做的办法是向每个雇员分配一张彩色,而其他雇员则从各种颜色中选取一个色。 最终,这名雇员将被分配到成为他们的圣殿秘密。 如下图所示,关于所分配的颜色和每名雇员所选取的颜色的资料已输入表格。 该表通过自食颜色栏自行取走圣秘密。 SQL Self Join 如表是两个表格,则表格本身也使用。 为了做到这一点,至少有一个表格暂时改名为KQ。 如果需要对同一表格的两栏进行比较,则自行加入为一类;可能要在两栏之间建立联系。 换言之,如果表内载有Foreign Key和Primary Key,则该表将并入。 然而,与其他缔约方不同的是,我们使用WHERE条款来具体规定表格与自己合并的条件;而不是《国家空间公约》条款。 The following is the basic syntax of Self Joinkou in 在这里,根据您的要求,WHERE条款可以表达。 单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单加入;这样,我们就应该制作一个客户表,列出他们的姓名、年龄、地址和工资等客户细节。 现在,在表格中添加以下价值观: 该表将编成: 现在,让我们利用以下自我问询加入这一表。 我们的目标是根据上述客户的收入建立关系。 我们是在WHERE条款的帮助下这样做的。 下表将列出收入低于其他客户的所有客户: 此外,在采用自成表格之后,合并表格中的记录也可以按照逐条排列的顺序排列。 The syntax for it - 让我们使用下表所示客户: 在执行以下询问时,我们将自行加入客户表格,加入《WHERE条款》;然后使用按条款对一栏的“ORDER”安排记录。 如上文所述,我们正在根据薪金栏安排记录。 下表所示为: 不仅薪金栏,记录可以按姓名的字母顺序、客户身份号码等分类。Self Join in SQL
Syntax
SELECT column_name(s)
FROM table1 a, table1 b
WHERE a.common_field = b.common_field;
Example
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, Ramesh , 32, Ahmedabad , 2000.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, Khilan , 25, Delhi , 1500.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, kaushik , 23, Kota , 2000.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, Chaitap , 25, Mumbai , 6500.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (5, Hardik , 27, Bhopal , 8500.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (6, Komal , 22, MP , 4500.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (7, Muffy , 24, Indore , 10000.00 );
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitap | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
SELECT a.ID, b.NAME as EARNS_HIGHER, a.NAME as EARNS_LESS, a.SALARY as LOWER_SALARY
FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY < b.SALARY;
Output
+----+--------------+------------+--------------+
| ID | EARNS_HIGHER | EARNS_LESS | LOWER_SALARY |
+----+--------------+------------+--------------+
| 2 | Ramesh | Khilan | 1500.00 |
| 2 | Kaushik | Khilan | 1500.00 |
| 6 | Chaitap | Komal | 4500.00 |
| 3 | Chaitap | Kaushik | 2000.00 |
| 2 | Chaitap | Khilan | 1500.00 |
| 1 | Chaitap | Ramesh | 2000.00 |
| 6 | Hardik | Komal | 4500.00 |
| 4 | Hardik | Chaitap | 6500.00 |
| 3 | Hardik | Kaushik | 2000.00 |
| 2 | Hardik | Khilan | 1500.00 |
| 1 | Hardik | Ramesh | 2000.00 |
| 3 | Komal | Kaushik | 2000.00 |
| 2 | Komal | Khilan | 1500.00 |
| 1 | Komal | Ramesh | 2000.00 |
| 6 | Muffy | Komal | 4500.00 |
| 5 | Muffy | Hardik | 8500.00 |
| 4 | Muffy | Chaitap | 6500.00 |
| 3 | Muffy | Kaushik | 2000.00 |
| 2 | Muffy | Khilan | 1500.00 |
| 1 | Muffy | Ramesh | 2000.00 |
+----+--------------+------------+--------------+
Self Join with ORDER BY Clause
Syntax
SELECT column_name(s)
FROM table1 a, table1 b
WHERE a.common_field = b.common_field
ORDER BY column_name;
Example
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitap | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
SELECT a.ID, b.NAME as EARNS_HIGHER, a.NAME as EARNS_LESS, a.SALARY as LOWER_SALARY
FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY < b.SALARY
ORDER BY a.SALARY;
Output
+----+--------------+------------+--------------+
| ID | EARNS_HIGHER | EARNS_LESS | LOWER_SALARY |
+----+--------------+------------+--------------+
| 2 | Ramesh | Khilan | 1500.00 |
| 2 | Kaushik | Khilan | 1500.00 |
| 2 | Chaitap | Khilan | 1500.00 |
| 2 | Hardik | Khilan | 1500.00 |
| 2 | Komal | Khilan | 1500.00 |
| 2 | Muffy | Khilan | 1500.00 |
| 3 | Chaitap | Kaushik | 2000.00 |
| 1 | Chaitap | Ramesh | 2000.00 |
| 3 | Hardik | Kaushik | 2000.00 |
| 1 | Hardik | Ramesh | 2000.00 |
| 3 | Komal | Kaushik | 2000.00 |
| 1 | Komal | Ramesh | 2000.00 |
| 3 | Muffy | Kaushik | 2000.00 |
| 1 | Muffy | Ramesh | 2000.00 |
| 6 | Chaitap | Komal | 4500.00 |
| 6 | Hardik | Komal | 4500.00 |
| 6 | Muffy | Komal | 4500.00 |
| 4 | Hardik | Chaitap | 6500.00 |
| 4 | Muffy | Chaitap | 6500.00 |
| 5 | Muffy | Hardik | 8500.00 |
+----+--------------+------------+--------------+