?
Solved

UPDATE a field from a COUNT in another table

Posted on 2009-07-16
4
Medium Priority
?
528 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 46

Accepted Solution

by:
Kent Olsen earned 375 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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. …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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