<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: MySQL UPDATE with SELECT on the same table in the same query</title>
	<atom:link href="http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/feed/" rel="self" type="application/rss+xml" />
	<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/</link>
	<description>Random developer notes</description>
	<lastBuildDate>Wed, 03 Aug 2011 09:35:01 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: WebDev</title>
		<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/comment-page-1/#comment-1595</link>
		<dc:creator>WebDev</dc:creator>
		<pubDate>Sat, 14 May 2011 17:18:15 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/?p=194#comment-1595</guid>
		<description>Thanks a lot! UPDATE queries are pretty obscure and poorly documented.</description>
		<content:encoded><![CDATA[<p>Thanks a lot! UPDATE queries are pretty obscure and poorly documented.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prodis a.k.a. Fernando Hamasaki de Amorim</title>
		<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/comment-page-1/#comment-1550</link>
		<dc:creator>Prodis a.k.a. Fernando Hamasaki de Amorim</dc:creator>
		<pubDate>Sun, 20 Mar 2011 11:09:42 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/?p=194#comment-1550</guid>
		<description>I&#039;ve had the same problem:
#1093 - You can&#039;t specify target table &#039;my_table&#039; for update in FROM clause

So, my first solution has been with variables (like zag):
SELECT @next_value := MAX(some_value) + 1 FROM my_table;
UPDATE my_table SET some_value = @next_value WHERE id = 123;

Now I&#039;m using your solution with Bruno&#039;s simplified approach. 

Thanks everybody.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve had the same problem:<br />
#1093 &#8211; You can&#8217;t specify target table &#8216;my_table&#8217; for update in FROM clause</p>
<p>So, my first solution has been with variables (like zag):<br />
SELECT @next_value := MAX(some_value) + 1 FROM my_table;<br />
UPDATE my_table SET some_value = @next_value WHERE id = 123;</p>
<p>Now I&#8217;m using your solution with Bruno&#8217;s simplified approach. </p>
<p>Thanks everybody.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zag</title>
		<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/comment-page-1/#comment-1478</link>
		<dc:creator>zag</dc:creator>
		<pubDate>Thu, 07 Oct 2010 00:28:40 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/?p=194#comment-1478</guid>
		<description>I had the same problem and I solved it by used variables:

select @a:= select MAX( some_value ) from my_table;
UPDATE my_table SET some_value = ( @a + 1 ) WHERE id = 123;</description>
		<content:encoded><![CDATA[<p>I had the same problem and I solved it by used variables:</p>
<p>select @a:= select MAX( some_value ) from my_table;<br />
UPDATE my_table SET some_value = ( @a + 1 ) WHERE id = 123;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gopi</title>
		<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/comment-page-1/#comment-1471</link>
		<dc:creator>gopi</dc:creator>
		<pubDate>Tue, 14 Sep 2010 23:31:19 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/?p=194#comment-1471</guid>
		<description>Guys, I too have been breaking my head on this err... thanks for Bruno&#039;s hint. I followed his &#039;aliasing&#039; approach to build a complex query to update moving averages of quantity.
Thanks Bruno !

I had a simple table t, with cols &#039;dt&#039; for date and &#039;qty&#039; for quantity. Here is how i built step by step:

step 1: //start with outline t5 is some random alias 

update t tmp () set tmp.mavg = t5.mavg where tmp.dt=t5.date;

step 2: //added t5 alias definition, which shud have mavg and date members.

update t tmp ( ()as t5) set tmp.mavg = t5.mavg where tmp.dt=t5.date;

step 3:  mavg and date aliases defined. new aliases t1 &amp; t2 are create to calculate moving average.

update t tmp ( ( select t1.dt as date, avg(t2.qty) as mavg ...)as t5) set tmp.mavg = t5.mavg where tmp.dt=t5.date;

final touch:


update t tmp, ((select t1.dt as date, avg(t2.qty) as mavg from t t1, t t2
		where t1.dt &gt;=&#039;2007-01-04&#039; AND t1.dt BETWEEN t2.dt AND t2.dt+4
	group by t1.dt order by t1.dt
) as t5) set tmp.mavg = t5.mavg where tmp.dt=t5.date;


yuppie...it worked !

hope this approach will solve someones headache too !</description>
		<content:encoded><![CDATA[<p>Guys, I too have been breaking my head on this err&#8230; thanks for Bruno&#8217;s hint. I followed his &#8216;aliasing&#8217; approach to build a complex query to update moving averages of quantity.<br />
Thanks Bruno !</p>
<p>I had a simple table t, with cols &#8216;dt&#8217; for date and &#8216;qty&#8217; for quantity. Here is how i built step by step:</p>
<p>step 1: //start with outline t5 is some random alias </p>
<p>update t tmp () set tmp.mavg = t5.mavg where tmp.dt=t5.date;</p>
<p>step 2: //added t5 alias definition, which shud have mavg and date members.</p>
<p>update t tmp ( ()as t5) set tmp.mavg = t5.mavg where tmp.dt=t5.date;</p>
<p>step 3:  mavg and date aliases defined. new aliases t1 &amp; t2 are create to calculate moving average.</p>
<p>update t tmp ( ( select t1.dt as date, avg(t2.qty) as mavg &#8230;)as t5) set tmp.mavg = t5.mavg where tmp.dt=t5.date;</p>
<p>final touch:</p>
<p>update t tmp, ((select t1.dt as date, avg(t2.qty) as mavg from t t1, t t2<br />
		where t1.dt &gt;=&#8217;2007-01-04&#8242; AND t1.dt BETWEEN t2.dt AND t2.dt+4<br />
	group by t1.dt order by t1.dt<br />
) as t5) set tmp.mavg = t5.mavg where tmp.dt=t5.date;</p>
<p>yuppie&#8230;it worked !</p>
<p>hope this approach will solve someones headache too !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: did not work</title>
		<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/comment-page-1/#comment-1445</link>
		<dc:creator>did not work</dc:creator>
		<pubDate>Fri, 09 Jul 2010 18:01:55 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/?p=194#comment-1445</guid>
		<description>This didn&#039;t work for me. I still get &quot;You can specify target table &#039;foo&#039; for update in FROM clause.&quot;</description>
		<content:encoded><![CDATA[<p>This didn&#8217;t work for me. I still get &#8220;You can specify target table &#8216;foo&#8217; for update in FROM clause.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich</title>
		<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/comment-page-1/#comment-1399</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Tue, 25 May 2010 20:36:29 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/?p=194#comment-1399</guid>
		<description>Thanks! I&#039;ve been struggling with this too.  Thanks to Bruno too!</description>
		<content:encoded><![CDATA[<p>Thanks! I&#8217;ve been struggling with this too.  Thanks to Bruno too!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruno Ng</title>
		<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/comment-page-1/#comment-1250</link>
		<dc:creator>Bruno Ng</dc:creator>
		<pubDate>Tue, 16 Jun 2009 15:31:59 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/?p=194#comment-1250</guid>
		<description>Here&#039;s the simplified version :
UPDATE my_table t, (SELECT MAX(my_value) + 1 AS selected_value FROM my_table) AS t2 SET t.my_value = t2.selected_value WHERE t.id = 123;</description>
		<content:encoded><![CDATA[<p>Here&#8217;s the simplified version :<br />
UPDATE my_table t, (SELECT MAX(my_value) + 1 AS selected_value FROM my_table) AS t2 SET t.my_value = t2.selected_value WHERE t.id = 123;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vighnesh</title>
		<link>http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/comment-page-1/#comment-1223</link>
		<dc:creator>Vighnesh</dc:creator>
		<pubDate>Fri, 29 May 2009 09:39:48 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/?p=194#comment-1223</guid>
		<description>Thanks this helped me. I needed same sort of solution.</description>
		<content:encoded><![CDATA[<p>Thanks this helped me. I needed same sort of solution.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

