MySQL中的join
时间: 2024-06-05 20:08:24 浏览: 86
语句有哪些类型?能否举例说明?
MySQL中的join语句分为以下几种类型:
1. Inner Join(内连接):只返回两个表中满足连接条件的记录。
例如:SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.customer_id;
2. Left Join(左连接):返回左表中所有记录以及与右表中满足连接条件的记录。
例如:SELECT customers.customer_name, orders.order_id
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id;
3. Right Join(右连接):返回右表中所有记录以及与左表中满足连接条件的记录。
例如:SELECT orders.order_id, customers.customer_name
FROM orders
RIGHT JOIN customers
ON orders.customer_id = customers.customer_id;
4. Full Join(全连接):返回左表和右表中所有记录以及满足连接条件的记录。
例如:SELECT customers.customer_name, orders.order_id
FROM customers
FULL OUTER JOIN orders
ON customers.customer_id = orders.customer_id;
阅读全文