鍍金池/ 教程/ 數(shù)據(jù)庫(kù)/ SQL自連接
SQL MID()函數(shù)
SQL CONCAT_WS()函數(shù)
SQL SOUNDEX()函數(shù)
SQL OCTET_LENGTH()函數(shù)
SQL CHARACTER_LENGTH()函數(shù)
SQL TO_DAYS()函數(shù)
SQL FROM_DAYS()函數(shù)
SQL SELECT查詢語(yǔ)句
SQL字符串函數(shù)
SQL MINUTE()函數(shù)
SQL EXP(X)函數(shù)
SQL LOCALTIMESTAMP和LOCALTIMESTAMP()函數(shù)
SQL INSERT()函數(shù)
數(shù)據(jù)庫(kù) - 第一范式(1NF)
SQL REVERSE()函數(shù)
SQL SECOND()函數(shù)
SQL ASCII(str)函數(shù)
SQL COS()函數(shù)
SQL丟棄或刪除數(shù)據(jù)庫(kù)(DROP DATABASE)
SQL AVG()函數(shù)
SQL GROUP BY(分組)
SQL SPACE()函數(shù)
SQL創(chuàng)建數(shù)據(jù)庫(kù)(CREATE DATABASE)
數(shù)據(jù)庫(kù) - 第三范式(3NF)
SQL YEAR()函數(shù)
SQL SIN(X)函數(shù)
SQL MIN()函數(shù)
SQL Distinct關(guān)鍵字
SQL NOT NULL約束
SQL LOG10(X)函數(shù)
SQL DISTINCT重復(fù)處理
SQL臨時(shí)表
SQL LTRIM()函數(shù)
SQL WHERE子句
SQL BIT_COUNT()函數(shù)
SQL INSTR()函數(shù)
SQL View(視圖)
SQL CURDATE()函數(shù)
SQL UNION子句/操作符
SQL SUM()函數(shù)
數(shù)據(jù)庫(kù) - 第二范式(2NF)
SQL UCASE()函數(shù)
SQL TIME_TO_SEC()函數(shù)
SQL TAN(X)函數(shù)
SQL ORDER BY排序子句
SQL語(yǔ)法
SQL克隆表
SQL POSITION()函數(shù)
SQL CHAR()函數(shù)
SQL索引約束
SQL DAY()函數(shù)
SQL Having子句
SQL PERIOD_ADD()函數(shù)
SQL POW()函數(shù)
SQL ATAN(X)函數(shù)
SQL WEEK()函數(shù)
SQL DATE_FORMAT()函數(shù)
SQL TIMEDIFF()函數(shù)
SQL INNER JOIN(內(nèi)部連接)
SQL RPAD()函數(shù)
SQL MONTHNAME()函數(shù)
SQL LOCALTIME和LOCALTIME()函數(shù)
SQL FULL JOIN(全連接)
SQL TIMESTAMPADD()函數(shù)
SQL ABS()函數(shù)
SQL操運(yùn)算符
SQL字符串CONCAT()函數(shù)
SQL TRUNCATE()函數(shù)
SQL排序結(jié)果
SQL GREATEST()函數(shù)
SQL DAYOFYEAR()函數(shù)
SQL SUBSTRING_INDEX()函數(shù)
SQL FORMAT()函數(shù)
SQL AND和OR運(yùn)算符
SQL LENGTH()函數(shù)
SQL MAKETIME()函數(shù)
SQL DATEDIFF()方法
SQL COT()函數(shù)
SQL ELT()函數(shù)
SQL LEFT JOIN(左連接)
SQL數(shù)據(jù)類型
SQL UPDATE更新查詢
SQL STR_TO_DATE()函數(shù)
SQL OCT()函數(shù)
SQL FLOOR(X)函數(shù)
SQL LOWER()函數(shù)
SQL MOD()函數(shù)
SQL LCASE()函數(shù)
SQL選擇數(shù)據(jù)庫(kù)(SELECT Database, USE語(yǔ)句)
SQL RDBMS概念
SQL EXTRACT()函數(shù)
SQL NULL值
SQL唯一約束
SQL HOUR()方法
SQL SIGN(X)函數(shù)
SQL DATE(expr)函數(shù)
SQL實(shí)用函數(shù)
SQL從現(xiàn)有表創(chuàng)建表
SQL DATE_SUB()方法
SQL子查詢
SQL TIME_FORMAT()函數(shù)
SQL REPLACE()函數(shù)
SQL LPAD()函數(shù)
SQL EXPORT_SET()函數(shù)
SQL EXCEPT子句
SQL FIELD()函數(shù)
SQL CONVERT_TZ()函數(shù)
SQL邏輯運(yùn)算符
SQL SEC_TO_TIME()函數(shù)
SQL QUOTE()函數(shù)
SQL CURRENT_TIMESTAMP()方法
SQL QUARTER()函數(shù)
SQL MAKE_SET()函數(shù)
SQL ADDTIME()函數(shù)
SQL MAKEDATE()函數(shù)
SQL HEX()函數(shù)
SQL LEFT()函數(shù)
SQL使用序列(自動(dòng)遞增)
SQL DEFAULT約束
SQL CURRENT_TIME()函數(shù)
SQL表達(dá)式
SQL TIMESTAMP()函數(shù)
SQL NOW()函數(shù)
SQL TOP,LIMIT,ROWNUM子句
SQL RIGHT JOIN(右連接)
SQL CONV()函數(shù)
SQL CHECK約束
SQL INTERVAL()函數(shù)
SQL事務(wù)
SQL STRCMP()函數(shù)
SQL UPPER()函數(shù)
SQL UNHEX()函數(shù)
SQL UTC_TIME()函數(shù)
SQL SUBTIME()函數(shù)
SQL ADDDATE()函數(shù)
SQL PERIOD_DIFF()函數(shù)
SQL REGEXP模式
SQL笛卡爾或交叉連接
SQL BIN(N)函數(shù)
SQL RADIANS()函數(shù)
SQL OCT(N)函數(shù)
SQL DAYNAME()函數(shù)
SQL FIND_IN_SET()函數(shù)
SQL日期函數(shù)
SQL LOAD_FILE()函數(shù)
SQL MAX()函數(shù)
SQL ROUND()函數(shù)
SQL LAST_DAY()函數(shù)
SQL SQRT()函數(shù)
SQL LOCATE()函數(shù)
SQL算術(shù)運(yùn)算符
SQL索引
SQL CONV(N,from_base,to_base)函數(shù)
SQL Join聯(lián)接
SQL LOG()函數(shù)
SQL數(shù)值函數(shù)
SQL BIT_OR()函數(shù)
SQL自連接
SQL SUBDATE()函數(shù)
SQL創(chuàng)建表(CREATE Table)
SQL DEGREES()函數(shù)
SQL注入
SQL ACOS()函數(shù)
SQL DATE_ADD()和DATE_SUB()方法
SQL YEARWEEK()函數(shù)
SQL BIT_LENGTH()函數(shù)
SQL刪除表(DROP或DELETE Table)
SQL教程
SQL STD()函數(shù)
SQL別名語(yǔ)法
SQL TIME()函數(shù)
SQL WEEKOFYEAR()函數(shù)
SQL SYSDATE()函數(shù)
SQL MICROSECOND()函數(shù)
SQL RDBMS數(shù)據(jù)庫(kù)
SQL DAYOFMONTH()函數(shù)
SQL ORD()函數(shù)
SQL SUBSTRING()函數(shù)
SQL INTERSECT子句
SQL RAND()函數(shù)
SQL ATAN2()函數(shù)
SQL TIMESTAMPDIFF()函數(shù)
SQL UTC_DATE()函數(shù)
SQL FROM_UNIXTIME()函數(shù)
SQL ALTER TABLE(修改表)
SQL FORMAT(X,D)函數(shù)
SQL UNIX_TIMESTAMP()函數(shù)
SQL BIT_AND()函數(shù)
SQL DAYOFWEEK()函數(shù)
SQL LIKE子句
SQL約束
SQL比較運(yùn)算符
SQL INSERT INTO插入查詢
SQL外鍵
SQL CONCAT()函數(shù)
SQL CEIL()函數(shù)
SQL LEAST()函數(shù)
SQL RIGHT()函數(shù)
SQL REPEAT()函數(shù)
SQL CURRENT_DATE()函數(shù)
SQL SQRT(X)函數(shù)
SQL DELETE刪除查詢
SQL CURTIME()函數(shù)
SQL TRUNCATE TABLE(截?cái)啾?
SQL UTC_TIMESTAMP()函數(shù)
SQL ASIN(X)函數(shù)
SQL主鍵
SQL CHAR_LENGTH()函數(shù)
SQL WEEKDAY()函數(shù)
SQL PI()函數(shù)
SQL MONTH()函數(shù)
SQL通配符運(yùn)算符
SQL TRIM()函數(shù)

SQL自連接

SQL自連接被用來(lái)聯(lián)接表本身以作為兩個(gè)表,暫時(shí)重命名,在SQL語(yǔ)句中至少有一個(gè)表。

語(yǔ)法

自連接的基本語(yǔ)法如下:

SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_field = b.common_field;

在這里,WHERE子句可以根據(jù)您的需要給定的任意表達(dá)式。

例子:

考慮下面的兩個(gè)表中,(a)CUSTOMERS表如下:

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

現(xiàn)在,讓我們用自連接這個(gè)表,如下所示:

SQL> SELECT  a.ID, b.NAME, a.SALARY
     FROM CUSTOMERS a, CUSTOMERS b
     WHERE a.SALARY < b.SALARY;

這將產(chǎn)生以下結(jié)果:

+----+----------+---------+
| ID | NAME     | SALARY  |
+----+----------+---------+
|  2 | Ramesh   | 1500.00 |
|  2 | kaushik  | 1500.00 |
|  1 | Chaitali | 2000.00 |
|  2 | Chaitali | 1500.00 |
|  3 | Chaitali | 2000.00 |
|  6 | Chaitali | 4500.00 |
|  1 | Hardik   | 2000.00 |
|  2 | Hardik   | 1500.00 |
|  3 | Hardik   | 2000.00 |
|  4 | Hardik   | 6500.00 |
|  6 | Hardik   | 4500.00 |
|  1 | Komal    | 2000.00 |
|  2 | Komal    | 1500.00 |
|  3 | Komal    | 2000.00 |上一篇:SQL UNHEX()函數(shù)下一篇:SQL MINUTE()函數(shù)