Solved

UPDATE a field from a COUNT in another table

Posted on 2009-07-16
4
524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unwanted output from my query 5 41
StoredProcedure to JSON query faulty syntax 2 26
Error in sql query statment. 21 47
Estimating my database size 7 13
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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