Comments on: How to write flexible INSERT and UPDATE statements in MySQL http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Cherri http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/#comment-19727 Cherri Tue, 15 Nov 2011 21:19:08 +0000 http://www.xaprb.com/blog/?p=103#comment-19727 table1:
a, b, c
(1, 5, 3),
(2, 5, 6),
(3, 4, 9);
table2:
d, e, f
(1, 1, 1),
(4, 4, 4),
(5, 5, 5);

If we had these tables related. lets say that table1 has foreign key the column d of table2. And we had a joined table like:

SELECT a,b,c,table2.e
FROM table1
CROSS JOIN table2
where table1.b= table2.d;

How can new values be inserted?

]]>
By: Marg http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/#comment-19711 Marg Sat, 22 Oct 2011 22:26:23 +0000 http://www.xaprb.com/blog/?p=103#comment-19711 update table Rating’s ratingdate+25 if stars >=4

I don’t think you can query and update the same table so I created rating2 table (has same data).

update rating
set ratingdate = (ratingdate+25)
where stars = select avg(rating2.stars)
from rating2

Any advice would be appreciated asap.
Table: Rating
rID mID stars ratingDate
201 101 2 2011-01-22
201 101 4 2011-01-27
202 106 4 Null
203 103 2 2011-01-20
203 108 4 2011-01-12
203 108 2 2011-01-30
204 101 3 2011-01-09
205 103 3 2011-01-27
205 104 2 2011-01-22
205 108 4 Null
206 107 3 2011-01-15
206 106 5 2011-01-19
207 107 5 2011-01-20
208 104 3 2011-01-02

]]>
By: Billy http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/#comment-19581 Billy Sat, 20 Aug 2011 17:05:28 +0000 http://www.xaprb.com/blog/?p=103#comment-19581 I met a similar problem which got me thinking and researching all the day.

My situation like this: I got sales reports from two channels. Both of them have same format: product_id, sales_volume, month

What I need to do is to import these csv files and combine them into a single report: product_id, channel1_sale, chanel2_sale, month.

There are lots of products. Channel 1 and channel 2 may have same and different products.

I finally use your solution of “Inserting new rows and updating existing rows” to solve it. Additionally I made another temp table with one additional field to judge if the data comes from channel 1 or 2.

At first I import to temp table, then I use your method to transfer data from temp data to final table, then I truncate the dump table.

It works like a charm!

Thank you Xaprb, your article really rocks even it was written years ago!

]]>
By: MYSQL Update if data exists else insert | 24/7 Software http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/#comment-18273 MYSQL Update if data exists else insert | 24/7 Software Sun, 09 May 2010 20:53:57 +0000 http://www.xaprb.com/blog/?p=103#comment-18273 [...] a link to how I did it. Share This This entry was written by admin, posted on May 9, 2010 [...]

]]>
By: Shared Items - August 2, 2009 « Jeetu’s Shared Memory http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/#comment-16712 Shared Items - August 2, 2009 « Jeetu’s Shared Memory Mon, 03 Aug 2009 03:38:01 +0000 http://www.xaprb.com/blog/?p=103#comment-16712 [...] How to write flexible INSERT and UPDATE statements in MySQL [...]

]]>