Solved

UPDATE a field from a COUNT in another table

Posted on 2009-07-16
4
519 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
Comment Utility
I want to JOIN by Afield2 and Bfield3
0
 
LVL 45

Accepted Solution

by:
Kdo earned 125 total points
Comment Utility
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
Comment Utility
sorry for any inconvenience, I just found the problem myself
thanx :)
0
 
LVL 17

Expert Comment

by:smozgur
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Oracle Pivot 2 32
SQL Syntax:  How to Find Commonality Among Similar Results 2 32
sQL pivot 9 39
Help with SQL Query 23 39
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

11 Experts available now in Live!

Get 1:1 Help Now