Mysql UPDATE with JOIN and WHERE [closed]

1

My tables

EMPREGADO   Cod_Emp, Nome_Emp
COMPANHIA   Cod_Comp, Nome_Comp
TRABALHA    Cod_Emp, Cod_Comp, Salário, Cod_Emp_Supervisor

I want to give a 10% increase to all test company supervisors.

UPDATE trabalha 
INNER JOIN companhia ON trabalha.Cod_Comp = companhia.Cod_Comp AND companhia.Nome_comp = 'teste'
SET salario = salario + (salario*0.10)
WHERE Cod_Emp IN (SELECT Cod_Emp_Supervisor FROM trabalha)
    
asked by anonymous 29.09.2016 / 02:08

1 answer

1
UPDATE
    TRABALHA T
INNER JOIN
    COMPANHIA C ON 
    (
        C.Cod_Comp = T.Cod_Comp
        AND
        C.Nome_comp = 'Teste'
        AND
        T.Cod_Emp = T.Cod_Emp_Supervisor
    )
SET
    T.Salario = T.Salario + (T.Salario * 0.10)

So I understand the column Cod_Emp_Supervisor in the WORK table indicates who is the supervisor of each employee within the COMPANY.

As I do not know the base or the system that loads it. I imagine that the Cod_Emp_Supervisor field will be populated with the supervisor code itself. Or, if it does not fill in, it is null, just replace the T.Cod_Emp = T.Cod_Emp_Supervisor clause for T.Cod_Emp_Supervisor IS NULL .

    
29.09.2016 / 03:12