-
内连接(INNER JOIN)
语法:SELECT 列名
FROM 表1
INNER JOIN 表2 ON 表1.列名 = 表2.列名;
示例:
SELECT 列名
FROM 表1
LEFT JOIN 表2 ON 表1.列名 = 表2.列名;
-
左外连接(LEFT JOIN)
语法SELECT 列名
FROM 表1
LEFT JOIN 表2 ON 表1.列名 = 表2.列名;
示例:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;
-
右外连接(RIGHT JOIN)
语法:SELECT 列名
FROM 表1
RIGHT JOIN 表2 ON 表1.列名 = 表2.列名;**
示例:
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;
-
全外连接(FULL JOIN)
语法:SELECT 列名
FROM 表1
LEFT JOIN 表2 ON 表1.列名 = 表2.列名
UNION
SELECT 列名
FROM 表1
RIGHT JOIN 表2 ON 表1.列名 = 表2.列名;
示例:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id
UNION
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;
-
交叉连接(CROSS JOIN)
语法:SELECT 列名
FROM 表1
CROSS JOIN 表2;
示例:
SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments;