<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to select from an update target in MySQL</title>
	<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/</link>
	<description>Stay curious!</description>
	<pubDate>Sat, 30 Aug 2008 05:06:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: sql_developer</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-15014</link>
		<author>sql_developer</author>
		<pubDate>Thu, 14 Aug 2008 22:33:47 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-15014</guid>
		<description>Thanks very much for this solution. I was having a hard time finding a portable way to do this.</description>
		<content:encoded><![CDATA[<p>Thanks very much for this solution. I was having a hard time finding a portable way to do this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eyal Carmi</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14651</link>
		<author>Eyal Carmi</author>
		<pubDate>Sat, 24 May 2008 13:18:52 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14651</guid>
		<description>Hi,
I want to raise a performance question regarding this solution and hope someone will have an answer to it.
I have a table with millions of rows, this table has a unique key: id.
For each row there are:
id, val_1,val_2, matchingRowId
I have already computed and updated the matchingRowId for each row and now i want to set each row's 'val_2' based on 'val_1' of the mathcing row.

Originally i used:

update myTable set val_2=
 (select T1.val_1 
  from myTable as T1 
  where T1.id=myTable.matchingRowId);

This of course failed but when i tried rewriting the query as you suggested to:

update myTable set val_2=
 (select T1.val_1 
  from (select * from myTable) as T1 
  where T1.id=myTable.matchingRowId);

This works but the performance is terrible (since myTable is very large). I then read your second paper but i am not able to optimize the query since the following:

update myTable set val_2=
 (select T1.val_1 
  from (select * 
        from myTable as T1 
        where T1.id=myTable.matchingRowId));

But got an error message saying that 'myTable' is not recognized in the inner loop.

Can anyone help?

Thanks</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I want to raise a performance question regarding this solution and hope someone will have an answer to it.<br />
I have a table with millions of rows, this table has a unique key: id.<br />
For each row there are:<br />
id, val_1,val_2, matchingRowId<br />
I have already computed and updated the matchingRowId for each row and now i want to set each row&#8217;s &#8216;val_2&#8242; based on &#8216;val_1&#8242; of the mathcing row.</p>
<p>Originally i used:</p>
<p>update myTable set val_2=<br />
 (select T1.val_1<br />
  from myTable as T1<br />
  where T1.id=myTable.matchingRowId);</p>
<p>This of course failed but when i tried rewriting the query as you suggested to:</p>
<p>update myTable set val_2=<br />
 (select T1.val_1<br />
  from (select * from myTable) as T1<br />
  where T1.id=myTable.matchingRowId);</p>
<p>This works but the performance is terrible (since myTable is very large). I then read your second paper but i am not able to optimize the query since the following:</p>
<p>update myTable set val_2=<br />
 (select T1.val_1<br />
  from (select *<br />
        from myTable as T1<br />
        where T1.id=myTable.matchingRowId));</p>
<p>But got an error message saying that &#8216;myTable&#8217; is not recognized in the inner loop.</p>
<p>Can anyone help?</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mebugus</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14625</link>
		<author>mebugus</author>
		<pubDate>Wed, 21 May 2008 04:24:24 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14625</guid>
		<description>Only Thanks for this hint.</description>
		<content:encoded><![CDATA[<p>Only Thanks for this hint.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mysticav</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14461</link>
		<author>mysticav</author>
		<pubDate>Mon, 21 Apr 2008 04:47:55 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14461</guid>
		<description>For a credit / debit/ balance table, this is an example:

INSERT INTO account set credit = 100, balance =ifnull((select balance from (select ((sum(credit)-sum(debit))) as balance from account) as x),0) 100

For instance, If you run twice the query, the table will be:

&#124; credit &#124;  debit &#124; balance
  100       0       100
  100       0       200</description>
		<content:encoded><![CDATA[<p>For a credit / debit/ balance table, this is an example:</p>
<p>INSERT INTO account set credit = 100, balance =ifnull((select balance from (select ((sum(credit)-sum(debit))) as balance from account) as x),0) 100</p>
<p>For instance, If you run twice the query, the table will be:</p>
<p>| credit |  debit | balance<br />
  100       0       100<br />
  100       0       200</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14422</link>
		<author>David</author>
		<pubDate>Wed, 09 Apr 2008 22:01:01 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14422</guid>
		<description>(^__^)
(_ _)
(^__^)
(_ _)

thank you x</description>
		<content:encoded><![CDATA[<p>(^__^)<br />
(_ _)<br />
(^__^)<br />
(_ _)</p>
<p>thank you x</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14365</link>
		<author>Xaprb</author>
		<pubDate>Sat, 29 Mar 2008 15:03:20 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14365</guid>
		<description>On how to understand how MySQL works?  The book I helped write, when it is published, will be the best.  http://www.oreilly.com/catalog/9780596101718/  Note that that's the second edition: the first edition is not as detailed.

Otherwise, I think Pro MySQL is a very good book, written by my friends Jay Pipes and Mike Kruckenberg.  http://www.apress.com/book/view/159059505X</description>
		<content:encoded><![CDATA[<p>On how to understand how MySQL works?  The book I helped write, when it is published, will be the best.  <a href="http://www.oreilly.com/catalog/9780596101718/" rel="nofollow">http://www.oreilly.com/catalog/9780596101718/</a>  Note that that&#8217;s the second edition: the first edition is not as detailed.</p>
<p>Otherwise, I think Pro MySQL is a very good book, written by my friends Jay Pipes and Mike Kruckenberg.  <a href="http://www.apress.com/book/view/159059505X" rel="nofollow">http://www.apress.com/book/view/159059505X</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: T1</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14364</link>
		<author>T1</author>
		<pubDate>Sat, 29 Mar 2008 13:54:31 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14364</guid>
		<description>What's a good book on this topic?</description>
		<content:encoded><![CDATA[<p>What&#8217;s a good book on this topic?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14215</link>
		<author>Andy</author>
		<pubDate>Thu, 14 Feb 2008 20:28:10 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-14215</guid>
		<description>This works great even for delete statements. I had a table with a linked id to another table but didn't create a cascade foreign key yet and had some entries from the top table deleted. So to add a foreign key, I had to delete all the entries from the linked table that had nothing linking to it so I did this:

DELETE FROM tbldetails WHERE ID IN (SELECT ID FROM (SELECT tbldetails.ID FROM tbldetails LEFT JOIN tbltop ON tbltop.ID=tbldetails.topid WHERE top IS NULL) AS X);

(I replaced the actual names that would make more sense to what I'm talking about, hopefully).  Although I would think this is a common thing and wonder if there's an easier way to do that.</description>
		<content:encoded><![CDATA[<p>This works great even for delete statements. I had a table with a linked id to another table but didn&#8217;t create a cascade foreign key yet and had some entries from the top table deleted. So to add a foreign key, I had to delete all the entries from the linked table that had nothing linking to it so I did this:</p>
<p>DELETE FROM tbldetails WHERE ID IN (SELECT ID FROM (SELECT tbldetails.ID FROM tbldetails LEFT JOIN tbltop ON tbltop.ID=tbldetails.topid WHERE top IS NULL) AS X);</p>
<p>(I replaced the actual names that would make more sense to what I&#8217;m talking about, hopefully).  Although I would think this is a common thing and wonder if there&#8217;s an easier way to do that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Barradas</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-13719</link>
		<author>Daniel Barradas</author>
		<pubDate>Wed, 28 Nov 2007 09:42:41 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-13719</guid>
		<description>This worked for me.

I needed to update a number with the max value of a column in the same table.

update inscricoes
   set dorsal = (
      select dorsal from (
         select max(dorsal) 1 as dorsal from inscricoes
      ) as dorsal)
   where id = '$id'

Do you see any problem that may arise from this approach? (with concurrency maybe?) 

Anyway ... thanks for sharing! ;)</description>
		<content:encoded><![CDATA[<p>This worked for me.</p>
<p>I needed to update a number with the max value of a column in the same table.</p>
<p>update inscricoes<br />
   set dorsal = (<br />
      select dorsal from (<br />
         select max(dorsal) 1 as dorsal from inscricoes<br />
      ) as dorsal)<br />
   where id = &#8216;$id&#8217;</p>
<p>Do you see any problem that may arise from this approach? (with concurrency maybe?) </p>
<p>Anyway &#8230; thanks for sharing! ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-13321</link>
		<author>Xaprb</author>
		<pubDate>Mon, 20 Aug 2007 12:18:45 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/#comment-13321</guid>
		<description>It looks like you can just rewrite that as a self-join instead of using subqueries.  This will probably work much better:

&lt;pre&gt;update rehabs as t1
   inner join rehabs as t2 using(name, address1,...)
set t1.phone2 = t2.phone&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>It looks like you can just rewrite that as a self-join instead of using subqueries.  This will probably work much better:</p>
<pre>update rehabs as t1
   inner join rehabs as t2 using(name, address1,...)
set t1.phone2 = t2.phone</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
