473,508 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a SUM in an UPDATE

mailing.database.mysql -

I'm trying to update a "parent" table with the sum of data in a "child
table". To illustrate, let's say our database was modeled around a book:

book
-> chapter
-> page
-> attachment (pictures)

The -> represents a one-to-many relationship (e.g. a book has chapters).
Each attachment has a filesize and the parent book has a total_size
(denormalized) column that is the sum of all it's children attachments. I'd
like a query to update total_size in the book table.

Below is what I've come up with thus far. Obviously, it's not working, and
hence my post. The nested SELECT query works fine, but I'm receiving a "You
can't specify target table 'book' for update in FROM clause" error. Please
let me know your thoughts.

update
book
set
total_size = (
select
sum(attachment.filesize)
from
attachment, page, chapter, book
where
attachment.page_id = page.page_id
and
page.chapter_id = chapter.chapter_id
and
chapter.book_id = book.book_id
and
book.book_id = '5'
)
where
book_id = '5'

--
Josh
Jul 23 '05 #1
1 7497
mailing.database.mysql -

Please ignore post. I've figured it out - I needed to remove "book" from
the inner FROM clause.

update
book
set
book.total_storage = (
select
sum(attachment.filesize)
from
attachment, page, chapter
where
attachment.page_id = page.page_id
and
page.chapter_id = chapter.chapter_id
and
chapter.book_id = book.book_id
and
book.book_id = '5'
)
where
book.book_id = '5'

--
Josh
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1418
by: Brian Henry | last post by:
I have a page which reads an article from the database it has 1 text box, 2 dropdown lists, and a longreat HTML text box. I load the information from the database when the page is set to edit mode...
3
3857
by: Michael Glass | last post by:
I'm working on an ASP.Net web app using VS2005 and the .Net 2.0 framework, and I have a serious problem with the page I'm currently working on. The page has, among other things, two FormViews and a...
6
1710
by: Brett | last post by:
Not sure what the problem is here... Trying to update from a datagrid to an access database using vb.net... Its not updating the database but Im not getting any errors... Here is my code... ...
1
4960
by: thangchan | last post by:
Hi all, i am getting SQL update problem. as below ======================error messages ======================= Server Error in '/CMS' Application....
1
1127
by: Bob | last post by:
I got a datagridview on which I do an update, the sace works fine, but after the save, the newly saved values do not get displayed in the cells. It looks like I need to do a refresh of some sort to...
0
1741
by: Erik | last post by:
Why isn't my update method getting called? Pasted below is an aspx from a 1.1 application I'm working on. It has two textboxes and a button for inserting data into the database, and a datagrid...
8
2389
by: ljungers | last post by:
Hi to all. Trying to get a onclick VBA routine to run, but I'm getting an error message when I click on the button that is used to run the VBA code to change/update a memo field in a table. This...
0
1334
by: JimN1 | last post by:
Error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows. This is a continuation of my previous table element update question. I am now getting the above...
10
1673
by: Mike | last post by:
I have code that is doing some updating to a record. Its getting the ID to update from the Grid. I'm passing an INT to my method to update the record. My code is working though I'm still getting an...
1
4900
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.