Unix Technical Forum

Multiple-table UPDATE unexpected result

This is a discussion on Multiple-table UPDATE unexpected result within the MySQL General forum forums, part of the MySQL category; --> Dear listmembers On mysql version 4.1.13 I execute a query of this type: UPDATE a LEFT JOIN b ON ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:45 AM
Thomas Spahni
 
Posts: n/a
Default Multiple-table UPDATE unexpected result

Dear listmembers

On mysql version 4.1.13 I execute a query of this type:

UPDATE a
LEFT JOIN b ON a.col = b.col
SET a.x = a.x + b.y
WHERE b.col IS NOT NULL;

I expect that column a.x is updated for every match in the join but this
is not the case. Table a is updated for the first match only as in this
example:

mysql> use test;
Database changed

mysql> create table atable ( a int, b int);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into atable values(1,10),(2,10),(3,10),(4,10);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> select * from atable;
+------+------+
| a | b |
+------+------+
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
| 4 | 10 |
+------+------+
4 rows in set (0.00 sec)

mysql> create table btable (a int, b int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into btable values(2,5),(3,6),(3,7);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from btable;
+------+------+
| a | b |
+------+------+
| 2 | 5 |
| 3 | 6 |
| 3 | 7 |
+------+------+
3 rows in set (0.00 sec)

mysql> update atable left join btable on atable.a = btable.a
set atable.b = atable.b + btable.b where btable.a is not null;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> select * from atable;
+------+------+
| a | b |
+------+------+
| 1 | 10 |
| 2 | 15 |
| 3 | 16 |
| 4 | 10 |
+------+------+
4 rows in set (0.00 sec)

However, the result I would like to achieve is (manually edited for the
purpose of explanation):

mysql> select * from atable;
+------+------+
| a | b |
+------+------+
| 1 | 10 |
| 2 | 15 |
| 3 | 23 |
| 4 | 10 |
+------+------+
4 rows in set (0.00 sec)

i.e. row 3 of atable should be updated 2 times, adding 6 and 7, as there
are 2 rows in btable where column a is = 3.

How can I do this? Any help is apreciated.

Thomas Spahni
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:46 AM
ViSolve DB Team
 
Posts: n/a
Default Re: Multiple-table UPDATE unexpected result

Hi,
Try this,

mysql> update atable,btable set atable.b=atable.b+(select sum(b) from btable
where btable.a=atable.a) where atable.a=btable.a;

mysql> select * from atable;
+------+------+
| a | b |
+------+------+
| 1 | 10 |
| 2 | 15 |
| 3 | 23 |
| 4 | 10 |
+------+------+
4 rows in set (0.00 sec)

Thanks
ViSolve DB Team.

----- Original Message -----
From: "Thomas Spahni" <tsp@lawbiz.ch>
To: <mysql@lists.mysql.com>
Cc: <tsp@lawbiz.ch>
Sent: Thursday, February 08, 2007 3:57 PM
Subject: Multiple-table UPDATE unexpected result


> Dear listmembers
>
> On mysql version 4.1.13 I execute a query of this type:
>
> UPDATE a
> LEFT JOIN b ON a.col = b.col
> SET a.x = a.x + b.y
> WHERE b.col IS NOT NULL;
>
> I expect that column a.x is updated for every match in the join but this
> is not the case. Table a is updated for the first match only as in this
> example:
>
> mysql> use test;
> Database changed
>
> mysql> create table atable ( a int, b int);
> Query OK, 0 rows affected (0.00 sec)
>
> mysql> insert into atable values(1,10),(2,10),(3,10),(4,10);
> Query OK, 4 rows affected (0.00 sec)
> Records: 4 Duplicates: 0 Warnings: 0
>
> mysql> select * from atable;
> +------+------+
> | a | b |
> +------+------+
> | 1 | 10 |
> | 2 | 10 |
> | 3 | 10 |
> | 4 | 10 |
> +------+------+
> 4 rows in set (0.00 sec)
>
> mysql> create table btable (a int, b int);
> Query OK, 0 rows affected (0.01 sec)
>
> mysql> insert into btable values(2,5),(3,6),(3,7);
> Query OK, 3 rows affected (0.00 sec)
> Records: 3 Duplicates: 0 Warnings: 0
>
> mysql> select * from btable;
> +------+------+
> | a | b |
> +------+------+
> | 2 | 5 |
> | 3 | 6 |
> | 3 | 7 |
> +------+------+
> 3 rows in set (0.00 sec)
>
> mysql> update atable left join btable on atable.a = btable.a
> set atable.b = atable.b + btable.b where btable.a is not null;
> Query OK, 2 rows affected (0.00 sec)
> Rows matched: 2 Changed: 2 Warnings: 0
>
> mysql> select * from atable;
> +------+------+
> | a | b |
> +------+------+
> | 1 | 10 |
> | 2 | 15 |
> | 3 | 16 |
> | 4 | 10 |
> +------+------+
> 4 rows in set (0.00 sec)
>
> However, the result I would like to achieve is (manually edited for the
> purpose of explanation):
>
> mysql> select * from atable;
> +------+------+
> | a | b |
> +------+------+
> | 1 | 10 |
> | 2 | 15 |
> | 3 | 23 |
> | 4 | 10 |
> +------+------+
> 4 rows in set (0.00 sec)
>
> i.e. row 3 of atable should be updated 2 times, adding 6 and 7, as there
> are 2 rows in btable where column a is = 3.
>
> How can I do this? Any help is apreciated.
>
> Thomas Spahni
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=m...rt@visolve.com
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:16 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com