1

Here is the structure of my database named employee

mysql> desc employee;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| emp_code    | int(11)       | NO   | PRI | NULL    |       |
| emp_name    | varchar(25)   | YES  |     | NULL    |       |
| designation | varchar(25)   | YES  |     | NULL    |       |
| department  | varchar(25)   | YES  |     | NULL    |       |
| basic       | decimal(10,2) | YES  |     | NULL    |       |
| DA          | decimal(10,2) | YES  |     | NULL    |       |
| gross_pay   | decimal(10,2) | YES  |     | NULL    |       |
+-------------+---------------+------+-----+---------+-------+

My question is, how can I update DA with value 1500 for rows with designation manager and update DA with value 2000 for all other employees.

1

4 Answers 4

1

You would use case for this purpose:

update employees
    set DA = (case when designation = 'manager' then 1500 else 2000 end);
Sign up to request clarification or add additional context in comments.

Comments

0

use CASE on update

UPDATE  employee
SET     DA =
        CASE
        WHEN designation = 'manager' THEN
                '1500'

        WHEN designation = 'employees' THEN
                '2000'
        END

Comments

0
UPDATE employee
SET DA = CASE 
WHEN designation = "manager" THEN 1500
ELSE 2000
END

Comments

0
UPDATE employee SET
DA = IF(designation='manager', 1500),
DA = IF(designation='', 1200)
WHERE designation IN('manager', '');

Did some research and constructed the above. Try it out, see what happens.

5 Comments

IF(designation='', 1200), thats the case when it is empty. The OP wants 1200 in any other case.
Aah, I see... so what would go in the place of ' '?
I'd say designation<>'manager', though i'm not sure if it works like this. (With 2x DA =)
We could throw in an "else" some where I pressume?
That would make more sence indeed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.