Solved

UPDATE a field from a COUNT in another table

Posted on 2009-07-16
4
526 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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