473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Multiple update question

Hi,

I have a mySql question here on updates to multiple tables.

Here's a simple schema to clarify things:
Structure

Table A
-------
Field A1
Field A2 (both int)

Table B
-------
Field B1
Field B2 (both int)
Data

Table A
-------
Row 1: (A1 - 1, A2 - 0)
Row 2: (A1 - 2, A2 - 0)
Table B
-------
Row 1: (B1 - 1, A2 - 2)
Row 2: (B1 - 1, A2 - 3)
Row 3: (B1 - 2, A2 - 4)
Row 4: (B1 - 2, A2 - 5)
Row 5: (B1 - 2, A2 - 6)
Now it is possible to group the rows in Table B in to sets where each set
has the same value of B1 (let's call these B1 groups). In our example, this
would be something like this:

B1 group with B1 field value of 1
(B1 - 1, A2 - 2)
(B1 - 1, A2 - 3)

B1 group with B1 field value of 2
(B1 - 2, A2 - 4)
(B1 - 2, A2 - 5)
(B1 - 2, A2 - 6)
I would like field A2 in Table A updated with a value that is obtained as
follows:

The query gets the value of the A1 field for the row whose A2 value it is to
update;

It looks at the B1 groups to see which group has the same B1 field value as
the A1 field value it got earlier;

It then gets the highest value for B2 for all the records in that B1 group;

It updates field A2 with this value.
For example, to update Row 1, the query will get the value of the field A1
for the row - this is 1.

It will then look at the B1 groups to see which group has the same B1 field
value as 1. There are two B1 groups here, as listed above. The first group
has all B1 fields having a value of 1. So it will pick this.

It will then look at all the B2 field values in this group, and see what the
highest value is. There are two field values - 2 and 3. So it will pick 3.

Lastly, it will update the field A2 in row 1 with this value of 3.
I hope this explains what I'm trying to achieve - sorry if it's a bit
longwinded.

I had a query that ran something like this, but it didn't quite work:

UPDATE A INNER JOIN B
ON A.A1 = B.B1
SET A.A2 = max(B.B2);

TIA,

--
Akin

aknak at aksoto dot idps dot co dot uk
Oct 30 '05 #1
3 1659
Hm...

No joy yet... I'll try crossposting to alt.php.
Epetruk wrote:
Hi,

I have a mySql question here on updates to multiple tables.

Here's a simple schema to clarify things:
Structure

Table A
-------
Field A1
Field A2 (both int)

Table B
-------
Field B1
Field B2 (both int)
Data

Table A
-------
Row 1: (A1 - 1, A2 - 0)
Row 2: (A1 - 2, A2 - 0)
Table B
-------
Row 1: (B1 - 1, A2 - 2)
Row 2: (B1 - 1, A2 - 3)
Row 3: (B1 - 2, A2 - 4)
Row 4: (B1 - 2, A2 - 5)
Row 5: (B1 - 2, A2 - 6)
Now it is possible to group the rows in Table B in to sets where each
set has the same value of B1 (let's call these B1 groups). In our
example, this would be something like this:

B1 group with B1 field value of 1
(B1 - 1, A2 - 2)
(B1 - 1, A2 - 3)

B1 group with B1 field value of 2
(B1 - 2, A2 - 4)
(B1 - 2, A2 - 5)
(B1 - 2, A2 - 6)
I would like field A2 in Table A updated with a value that is
obtained as follows:

The query gets the value of the A1 field for the row whose A2 value
it is to update;

It looks at the B1 groups to see which group has the same B1 field
value as the A1 field value it got earlier;

It then gets the highest value for B2 for all the records in that B1
group;

It updates field A2 with this value.
For example, to update Row 1, the query will get the value of the
field A1 for the row - this is 1.

It will then look at the B1 groups to see which group has the same B1
field value as 1. There are two B1 groups here, as listed above. The
first group has all B1 fields having a value of 1. So it will pick
this.

It will then look at all the B2 field values in this group, and see
what the highest value is. There are two field values - 2 and 3. So
it will pick 3.

Lastly, it will update the field A2 in row 1 with this value of 3.
I hope this explains what I'm trying to achieve - sorry if it's a bit
longwinded.

I had a query that ran something like this, but it didn't quite work:

UPDATE A INNER JOIN B
ON A.A1 = B.B1
SET A.A2 = max(B.B2);

TIA,

Oct 31 '05 #2
Epetruk wrote:
Hm...

No joy yet... I'll try crossposting to alt.php.
Cross posting to alt.php.sql had been a lot better.
Epetruk wrote:
Hi,

I have a mySql question here on updates to multiple tables.

Here's a simple schema to clarify things:
Structure

Table A
-------
Field A1
Field A2 (both int)

Table B
-------
Field B1
Field B2 (both int)
Data

Table A
-------
Row 1: (A1 - 1, A2 - 0)
Row 2: (A1 - 2, A2 - 0)
Table B
-------
Row 1: (B1 - 1, A2 - 2)
Row 2: (B1 - 1, A2 - 3)
Row 3: (B1 - 2, A2 - 4)
Row 4: (B1 - 2, A2 - 5)
Row 5: (B1 - 2, A2 - 6)
Now it is possible to group the rows in Table B in to sets where each
set has the same value of B1 (let's call these B1 groups). In our
example, this would be something like this:

B1 group with B1 field value of 1
(B1 - 1, A2 - 2)
(B1 - 1, A2 - 3)

B1 group with B1 field value of 2
(B1 - 2, A2 - 4)
(B1 - 2, A2 - 5)
(B1 - 2, A2 - 6)
I would like field A2 in Table A updated with a value that is
obtained as follows:

The query gets the value of the A1 field for the row whose A2 value
it is to update;

It looks at the B1 groups to see which group has the same B1 field
value as the A1 field value it got earlier;

It then gets the highest value for B2 for all the records in that B1
group;

It updates field A2 with this value.
For example, to update Row 1, the query will get the value of the
field A1 for the row - this is 1.

It will then look at the B1 groups to see which group has the same B1
field value as 1. There are two B1 groups here, as listed above. The
first group has all B1 fields having a value of 1. So it will pick
this.

It will then look at all the B2 field values in this group, and see
what the highest value is. There are two field values - 2 and 3. So
it will pick 3.

Lastly, it will update the field A2 in row 1 with this value of 3.
I hope this explains what I'm trying to achieve - sorry if it's a bit
longwinded.

I had a query that ran something like this, but it didn't quite work:

UPDATE A INNER JOIN B
ON A.A1 = B.B1
SET A.A2 = max(B.B2);

TIA,


Nov 1 '05 #3
Try this:

update A, (select B.b1, max(B.b2) b2
from B
group by B.b1
) as x
set A.a2 = x.b2
where A.a1 = x.b1

Nov 1 '05 #4

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

Similar topics

7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
2
by: beena | last post by:
Apologize for posting this question.... Yes there were postings on update with join.... My question involves 4 table join... (hopefully qualifies as a new question) Need to convert the...
3
by: Tc | last post by:
Hi, I was curious, I am thinking of writing an application that loads a dataset from a database that resides on a server. The question I have is this, if multiple copies of the app will be...
1
by: Justin | last post by:
What is the best/easiest way to update multiple tables at a time? Can you recommend any tutorials? Thanks, Justin.
5
by: mimo | last post by:
Hello, I have seen samples on how to pull data from one table and save back to it using the Form View control. How do I pull from multiple tables and save back to multiple tables on one...
3
by: spartacus | last post by:
Hi, Does SQL support update to multiple rows where values coming from a sub-query? e.g insert into TABLE1 select column1, column2, column3 from TABLE2
10
by: Marc R. | last post by:
Hi all, I edit records using a form that have multiple control bind on Dataview, But I don't want to update right always to database, I would like to delay until all Changes (add all new...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
8
by: puzzlecracker | last post by:
Say you have a class: stuct Updater{ int _v1; int _v2 int _v3; update(int v1, int v2, int v2); bool isBad(int val); };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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,...

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.