Self join in Mysql : Mysql Joins
Posted by Raj
Self join in Mysql:
Self join is a method of centralizing relational data to a single table.A self-join joins the table to itself.
Example:
CREATE TABLE employees (
NAME VARCHAR(20),
email VARCHAR(20),
mobile VARCHAR(20),
sex CHAR(1),
birth_date DATE,
);
INSERT INTO employees VALUES ('xyz','xyz@example.com','11212','F','1985-06-05');
INSERT INTO employees VALUES ('abc','abc@example.com','11111','M','1985-03-05');
INSERT INTO employees VALUES ('abc1','abc1@example.com','21111','M','1987-03-05');
SELECT e1.name, e2.name,e1.email
FROM employees AS e1, employees AS e2
WHERE e1.email = e2.email AND e1.sex = 'F' AND e2.sex = 'M';
Self join is a method of centralizing relational data to a single table.A self-join joins the table to itself.
Example:
CREATE TABLE employees (
NAME VARCHAR(20),
email VARCHAR(20),
mobile VARCHAR(20),
sex CHAR(1),
birth_date DATE,
);
INSERT INTO employees VALUES ('xyz','xyz@example.com','11212','F','1985-06-05');
INSERT INTO employees VALUES ('abc','abc@example.com','11111','M','1985-03-05');
INSERT INTO employees VALUES ('abc1','abc1@example.com','21111','M','1987-03-05');
SELECT e1.name, e2.name,e1.email
FROM employees AS e1, employees AS e2
WHERE e1.email = e2.email AND e1.sex = 'F' AND e2.sex = 'M';
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
mysql self join,
mysql self join example,
self join,
self join example,
self join in sql,
self join sql,
self joins
.You can leave a response, or trackback from your own site.