Jump to content

Recommended Posts

Hi there,

 

I need to update field and set it from an another fields grouped avg. But can i use sub queries that produce multiple rows in SET?

example i need to select this

SELECT AVG(field1) from table1 Group BY field2

 

and update it into table1.field3

Is it possible, i know i can do it with php by first retreive it then update?

 

but for that i wud to make a query for each row.

what is an efficient solution to this?

Link to comment
https://forums.phpfreaks.com/topic/39276-sub-query-on-update-table/
Share on other sites

unless you have a good reason to do so.

 

That's true to a point. If you've got a small dataset, and it takes a fraction of a second to calculate avg then don't bother storing it. A good reason to do so would be when you have hundreds of thousands of records that you need to average.

 

I'm not entirely clear on what you are trying to do, but it sounds like you need a where clause so your subquery only returns a single row for the info you are looking for.

I hate storing redundant data.

 

True. But the client and the end user could care less about what goes on on the back end. They just know that when given the choice between a page that loads in 11 seconds and one that loads in .4 seconds, they like the quicker one.

 

But I don't think you can categorically say that one method is right or wrong. I've had some projects where calculating aggregate data on the fly was the right solution, other projects where caching data structures in PHP was the solution, and projects where caching the info in a database was definitely the right answer.

I would question your decision to keep dynamic data stored in a table, unless you have a good reason to do so.

Good question. But the client wants it that way.

Everyone's talking about why this shud be done. I want to know how it should be done.

Does the Update keyword accepts several row of data?

Same question goes for Insert keyword also.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.