I have 2 tables: Equipment
, and Equipment_Type
:
+-------------------------------+
| EqId [PK] | ETId [FK] | EqNum |
+-------------------------------+
| 1 | 1 | ABC |
| 2 | 1 | DEF |
| 3 | 3 | GHI |
+-------------------------------+
| ETId [PK] | Code | Discipline |
+-------------------------------+
| 1 | MOT | ELEC |
| 2 | MOT | MECH |
| 3 | SW | ELEC |
So from this example, we can see that both of our equipment are electrical motors.
However, due to a misunderstanding in the initial population, all of the equipment types were identified a ELEC
disciplines. Since then, the MECH
equipment has been identified, and I have to find all of the equipment that has been duplicated in the Equipment_Type
table, and change them to reference the MECH
equipment types instead.
I tried this:
SELECT * FROM Equipment EQ
INNER JOIN Equipment_Type ET on ET.ETId = EQ.ETId
WHERE ET.Discipline = 'MECH';
Which (obviously) returns no results - as with all the other JOIN
queries.
What I want to achieve is a search that will select only the Equipment that has an ELEC
Equipment Type that is also a MECH
equipment type. I realise this requires a nested query, but I'm not sure where to place it.
So the search should return:
+---------------------------+
| EqNum | ETId | Discipline |
+---------------------------+
| DEF | 1 | ELEC |
Because that entry needs to be changed to the MECH
discipline (i.e. ETId = 2 instead of 1)