Solved

Updating a value in one row to a value in another row of same table in mysql

Posted on 2014-03-12
13
226 Views
Last Modified: 2014-03-18
I have a customers table and want to update a value in (customer ID=x) col=a to (customer ID=y) col=c.
Current value in col a for customer ID=x is 1 and current value in col c for customer ID=y is 2

I want to run a query to update the 2 value in customer y (column name = Count) to be the same as the 1 value in customer x (column name = Subcount)
0
Comment
Question by:dynorich
  • 5
  • 5
13 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39923438
I had written an article about update/join:
http://www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html

this should help to solve this: watch out that mySQL is a bit differently than the other dbms in regards to the syntax.
0
 

Author Comment

by:dynorich
ID: 39923796
Didn't see solution.

column names: cid   Count   Sponsorscount
              values:  61         1                 0
                            46          0                2
I need to update Sponsorscount from 2 to equal 1 (the value of Count in cid 61)

How would I write the code below to accomplish this?
UPDATE customers
SET Sponsorscount = Count
WHERE cid=46, cid=61
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39923823
let's see if this works for you:
UPDATE customers 
  set Sponsorscount  = ( select o.`count` from customers o where o.cid = 61 )
WHERE cid = 46 

Open in new window

0
 

Author Comment

by:dynorich
ID: 39923854
Got the following error

#1093 - You can't specify target table 'customers' for update in FROM clause
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39923870
I remember that "issue" now, you cannot update, in mySql, from within the same table.
so, you have to run the query for the "select" part, store the results, and update using the intermedite results.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:dynorich
ID: 39924318
I've requested that this question be closed as follows:

Accepted answer: 0 points for dynorich's comment #a39923796

for the following reason:

Good response time and got the reason for my diffiulties and pointed me in another more doable direction.

Thank you
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39924319
Please close using my comments
0
 

Author Comment

by:dynorich
ID: 39933407
Solved the issue by creating a column in another table and then transfering  the value between the two tables.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39936219
I repeat that I commented that "in mysql, you cannot directly update a table from other rows from the same table" so one needs to first fetch the value to somewhere else, and then use it from there."
if that's a php variable, using a helper function, doing like the Dynorich posted, or whatever else, my comment is the "answer" to the original question: you cannot do it in a single step (like in all other dbms I know)
0
 

Author Comment

by:dynorich
ID: 39938410
I thought I had closed the question with thanks for the help but have had my question asking ability locked. I don't understand what I did wrong but thanks anyways
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Left Join with Tuple returning more rows 10 90
Oracle Pivot Question 8 45
myqsl update statement on phpMyAdmin 8 22
change database name 2 0
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now