简体   繁体   中英

Unable to update a Value as the Sum of two existing values in the same table (MySQL)

This is the first time that I am asking a question in Stack Overflow..

Anyways, I had a test earlier this week, regarding my skills in using SQL.. And, a particular question stumped me completely.. It was regarding using 'UPDATE' command.

The question was 'Update the salary of Vijay with the Sum of his and Sunil's salary...

Below is the table which was used for this purpose..

mysql> SELECT * FROM emp_company;
+--------+-------+----------+------------+
| ename  | cname | salary   | jdate      |
+--------+-------+----------+------------+
| Sunil  | ACC   | 15000.00 | 2003-12-12 |
| Vijay  | ACC   | 21000.00 | 2011-06-11 |
| Ishi   | TATA  | 40000.00 | 2011-03-14 |
| Mahesh | GMM   |  9000.00 | 2015-01-01 |
| Anjali | WIPRO | 25000.00 | 2013-04-21 |
+--------+-------+----------+------------+
5 rows in set (0.13 sec)

So,

DESIRED RESULT: I need to set Vijay's salary as the sum of both his and Sunil's which should come out to be 36000 ..

I used this particular query to answer it but failed...

UPDATE emp_company 
-> SET salary = (SELECT SUM(salary)
->               FROM emp_company
->               WHERE ename = 'Sunil' OR ename = 'Vijay')
-> WHERE ename = 'Vijay';

But, the following error message showed me that I failed:

ERROR 1093 (HY000): You can't specify target table 'emp_company' for update in FROM clause

Also, after reading some other discussions in StackOverflow, which I felt could answer my question, I tried this particular query...

mysql> UPDATE emp_company EC1, (SELECT SUM(salary)
    ->                          FROM emp_company
    ->                          WHERE ename = 'Sunil' OR ename = 'Vijay') EC2
    -> SET EC1.salary = EC2.salary
    -> WHERE EC1.name = 'Vijay';

Again, an error messaged popped up, informing me about my failure.. This time, the statement was a bit different:

ERROR 1054 (42S22): Unknown column 'EC1.name' in 'where clause'

So, it will be great help if someone could guide me or bring me one step closer to a solution..

Also, when I checked the subquery, it was showing the desired result..

mysql> SELECT SUM(salary)
    -> FROM emp_company
    -> WHERE ename = 'Vijay' OR ename = 'Sunil';
+-------------+
| SUM(salary) |
+-------------+
|    36000.00 |
+-------------+
1 row in set (0.01 sec)

PS1: I am an undergraduate student pursuing B.Tech in Computer Science..

PS2: Since, I am new to this forum (Not technically, since I have been using this site looking for answers to a lot of my problems), but this is the first time that I have posted a question.. So, please be gentle and inform me if I made any kind of mistake while framing the question..

Sorry, for posting so much information.. But, while posting the question, it was written to give as much as info as possible, so I did...

Do a update join like below but you better be having an key column like ID against which you will perform the comparison since name may have duplicate and in which case you will end up updating wrong records.

UPDATE emp_company ec
JOIN (SELECT ename, SUM(salary) AS targetsal
              FROM emp_company
              WHERE ename = 'Sunil' OR ename = 'Vijay') xx ON ec.ename = xx.ename
SET ec.salary = xx.targetsal
WHERE ec.ename = 'Vijay';

You can add another SELECT as shown below:

UPDATE emp_company 
SET salary = (SELECT salary from (select SUM(salary) as salary
           FROM emp_company
           WHERE ename = 'Sunil' OR ename = 'Vijay') t)
WHERE ename = 'Vijay';

Check my sqlfiddle: http://sqlfiddle.com/#!9/ea8719/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM