In MySQL, the keyword used for self-join is JOIN...ON. Self-joins are used to: 1. Find duplicate values; 2. Find related records; 3. Hierarchy modeling; 4. Find circular dependencies. For example, the following self-join query finds duplicate email addresses in the customers table: SELECT email_address FROM customers AS c1 JOIN customers AS c2 ON c1.email_address = c2.email_address WHERE c1.customer_id <> c2.customer_id;
Self-join keywords in MySQL
Self-join is a SQL operation that allows a table to be connected to itself. In MySQL, the keyword used to perform a self-join is JOIN...ON.
Self-join syntax:
<code class="sql">SELECT ... FROM table_name AS alias1 JOIN table_name AS alias2 ON alias1.column_name = alias2.column_name</code>
Among them:
Uses of self-join:
Self-join is mainly used for the following purposes:Example:
The following example demonstrates how to use a self-join to find duplicate email addresses in the Customers table:<code class="sql">SELECT email_address FROM customers AS c1 JOIN customers AS c2 ON c1.email_address = c2.email_address WHERE c1.customer_id <> c2.customer_id;</code>
The above is the detailed content of What is the keyword for self-connection in mysql?. For more information, please follow other related articles on the PHP Chinese website!