Solved

UPDATE a field from a COUNT in another table

Posted on 2009-07-16
4
523 Views
Last Modified: 2012-05-07
Hi

I want to do something like this:

UPDATE tableA SET Afield1 =(SELECT COUNT(Bfield1) FROM tableB WHERE NOT Bfield2 GROUP BY Bfield3)

But 2 problems:
1) Its returning an error telling that I have 2 returning rows of subquery, and
2) I see a problem because where is the join? It should update Afield1 for each Arecord based on a count in Btable. There have to be a connection between the 2 tables via the primary & foreign key IDs in each table.

How should my MySQL update statement look in order to work?
0
Comment
Question by:loopstudio
  • 2
4 Comments
 

Author Comment

by:loopstudio
ID: 24871029
I want to JOIN by Afield2 and Bfield3
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 125 total points
ID: 24871129
Hi loop,

The subselect in your update will return a row for every value of Bfield3 in tableB.  The query doesn't know how to match the result to tableA so it does the only thing it know to do -- attempt to update TableA.  But since the query returned multiple row, the DBMS is trying to store multiple rows in a single variabe.  Hence the error.

In the subquery, join the tables.  WHERE tableA.Afield = tableB.Bfield3.  The rest of the query looks fine.


Good Luck,
Kent
0
 

Author Comment

by:loopstudio
ID: 24871140
sorry for any inconvenience, I just found the problem myself
thanx :)
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 24872009
To moderator's review:
Provided solution by Kent is correct and it provides exact solution to the problem.
However, I see loop posted cancel request at the same time answer provided by Kent but didn't provide the found solution.

There is no point to keep this question in the database in case it wouldn't point to a solution. Therefore my suggestion would be accepting Kent's solution with 0 point (unless loop comes up with something else) but refunding points. This way, people searching for "update description by using join" would be able to see a nice comment.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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