I am confused on when putting parts of the WHERE clause inside the ON of a JOIN would not work and why.
I think I have read somewhere that it is best to put clauses as part of the ON clause.
I tried the following (trivial example):
In the following I have noticed that for an inner join it will join on columns on the same name but this causes an error for a left join.
Then I noticed that the ON (Employee.sdetails_id=SalaryDetails.sdetails_id and Employee.status<>5)
does not filter anything with status = 5. I thought that it would be equivalent to ON (Employee.sdetails_id=SalaryDetails.sdetails_id) WHERE Employee.status<>5
but it is not.
If I modify the Employee table and make as primary key primary key (id, status)
then I get the same issue.
Can someone explain how does the ON work and why in this case the status does not filter anything even if it is part of the primary key?
mysql> select * from Employee JOIN SalaryDetails;
+----+------+--------+--------+-------------+-------------+-----------------------------------------------------+
| id | name | status | salary | sdetails_id | sdetails_id | details |
+----+------+--------+--------+-------------+-------------+-----------------------------------------------------+
| 1 | John | 0 | 1000 | 1 | 1 | Hired with the contract of a perm |
| 1 | John | 0 | 1000 | 1 | 2 | Hired with perm contract and salary to be increased |
| 2 | Jim | 0 | 1200 | 1 | 1 | Hired with the contract of a perm |
| 2 | Jim | 0 | 1200 | 1 | 2 | Hired with perm contract and salary to be increased |
| 3 | Nick | 5 | 1500 | 2 | 1 | Hired with the contract of a perm |
| 3 | Nick | 5 | 1500 | 2 | 2 | Hired with perm contract and salary to be increased |
+----+------+--------+--------+-------------+-------------+-----------------------------------------------------+
6 rows in set (0.00 sec)
mysql> select * from Employee LEFT JOIN SalaryDetails;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> select * from Employee LEFT JOIN SalaryDetails ON (Employee.sdetails_id=SalaryDetails.sdetails_id);
+----+------+--------+--------+-------------+-------------+-----------------------------------------------------+
| id | name | status | salary | sdetails_id | sdetails_id | details |
+----+------+--------+--------+-------------+-------------+-----------------------------------------------------+
| 1 | John | 0 | 1000 | 1 | 1 | Hired with the contract of a perm |
| 2 | Jim | 0 | 1200 | 1 | 1 | Hired with the contract of a perm |
| 3 | Nick | 5 | 1500 | 2 | 2 | Hired with perm contract and salary to be increased |
+----+------+--------+--------+-------------+-------------+-----------------------------------------------------+
3 rows in set (0.00 sec)
mysql> select * from Employee LEFT JOIN SalaryDetails ON (Employee.sdetails_id=SalaryDetails.sdetails_id and Employee.status<>5);
+----+------+--------+--------+-------------+-------------+-----------------------------------+
| id | name | status | salary | sdetails_id | sdetails_id | details |
+----+------+--------+--------+-------------+-------------+-----------------------------------+
| 1 | John | 0 | 1000 | 1 | 1 | Hired with the contract of a perm |
| 2 | Jim | 0 | 1200 | 1 | 1 | Hired with the contract of a perm |
| 3 | Nick | 5 | 1500 | 2 | NULL | NULL |
+----+------+--------+--------+-------------+-------------+-----------------------------------+
3 rows in set (0.00 sec)
mysql> select * from Employee LEFT JOIN SalaryDetails ON (Employee.sdetails_id=SalaryDetails.sdetails_id) where Employee.status<>5;
+----+------+--------+--------+-------------+-------------+-----------------------------------+
| id | name | status | salary | sdetails_id | sdetails_id | details |
+----+------+--------+--------+-------------+-------------+-----------------------------------+
| 1 | John | 0 | 1000 | 1 | 1 | Hired with the contract of a perm |
| 2 | Jim | 0 | 1200 | 1 | 1 | Hired with the contract of a perm |
+----+------+--------+--------+-------------+-------------+-----------------------------------+
2 rows in set (0.00 sec)
mysql> show create table Employee;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Employee | CREATE TABLE `Employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`salary` decimal(5,0) DEFAULT NULL,
`sdetails_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table SalaryDetails;
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SalaryDetails | CREATE TABLE `SalaryDetails` (
`sdetails_id` int(11) NOT NULL DEFAULT '0',
`details` text,
PRIMARY KEY (`sdetails_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from Employee;
+----+------+--------+--------+-------------+
| id | name | status | salary | sdetails_id |
+----+------+--------+--------+-------------+
| 1 | John | 0 | 1000 | 1 |
| 2 | Jim | 0 | 1200 | 1 |
| 3 | Nick | 5 | 1500 | 2 |
+----+------+--------+--------+-------------+
3 rows in set (0.00 sec)
mysql> select * from SalaryDetails;
+-------------+-----------------------------------------------------+
| sdetails_id | details |
+-------------+-----------------------------------------------------+
| 1 | Hired with the contract of a perm |
| 2 | Hired with perm contract and salary to be increased |
+-------------+-----------------------------------------------------+
2 rows in set (0.00 sec)