skip to Main Content

I have these values(mentioned BELOW) which i’m trying to insert in (newly made) salary column of employees table. Salary column is INT NOT NULL DEFAULT 25000.
37000, 32000, 35000, 45000, 55000, 37000.

I have got these values for emp_id column which are already there
101, 102, 103, 104, 105, 106

I’ve tried these syntax

INSERT INTO employees(Salary)
 VALUES (37000), (32000), (35000), (45000), (55000), (37000);

and

ALTER table employees INSERT INTO Salary VALUES (37000, 32000, 35000, 45000, 55000, 37000);

and many more but won’t embarrass myself more xD

[TABLE employees looks like this on display as of now]

2

Answers


  1. Use UPDATE to modify the contents of existing rows. Use CASE to specify the value for each employee ID.

    UPDATE employees
    SET Salary = CASE emp_id
        WHEN 101 THEN 37000
        WHEN 102 THEN 32000
        WHEN 103 THEN 35000
        -- and so on for all employee IDs
        ELSE Salary -- leave unchanged if not in the above list
    END
    
    Login or Signup to reply.
  2. Use UPDATE. Provide the id-salary values pairs as a datasource:

    UPDATE employees
    JOIN ( SELECT 101, 37000 UNION ALL
           SELECT 102, 32000 UNION ALL
           SELECT 103, 35000 UNION ALL
           SELECT 104, 45000 UNION ALL
           SELECT 105, 55000 UNION ALL
           SELECT 106, 37000) AS new_salary (emp_id, salary) USING (emp_id)
    SET employees.salary = new_salary.salary;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search