<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dan.thoughts &#187; auto</title>
	<atom:link href="http://blog.sosedoff.com/tag/auto/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sosedoff.com</link>
	<description>Web-development, PHP, Ruby, Sinatra, Merb, Rails, MySQL, SQLite, Web Services.</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:54:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Updating order fields in table with MySQL</title>
		<link>http://blog.sosedoff.com/2009/02/05/updating-order-fields-in-table-with-mysql/</link>
		<comments>http://blog.sosedoff.com/2009/02/05/updating-order-fields-in-table-with-mysql/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 04:00:54 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[auto]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[order]]></category>
		<category><![CDATA[transaction]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=50</guid>
		<description><![CDATA[This is a simple piece of code which describing how to update special order field in table. For example, you have table &#8216;photos&#8217;:

+ --------------------------------- +
&#124; ID    &#124; UserID &#124; OrderID &#124; ImgURL &#124;
+ --------------------------------- +
&#124; 1132  &#124; 1      &#124; 0       [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple piece of code which describing how to update special order field in table. For example, you have table &#8216;photos&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">+ --------------------------------- +
| ID    | UserID | OrderID | ImgURL |
+ --------------------------------- +
| 1132  | 1      | 0       | ...    |
| 2124  | 1      | 0       | ...    |
| 3456  | 1      | 0       | ...    |
.....................................
| N     | 1      | 0       | ...    |
+ --------------------------------- +</pre></div></div>

<p>OrderID field value by default is 0. If some images has been deleted order lose its consistency.<br />
To repair order we can use single transaction:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN;
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @<span style="color: #993333; font-weight: bold;">ORDER</span> :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> photos
<span style="color: #993333; font-weight: bold;">SET</span> order_id <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">ORDER</span> :<span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> user_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
COMMIT;</pre></div></div>

<p>After executing this on table &#8216;images&#8217; we`ll get such result:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">+ --------------------------------- +
| ID    | UserID | OrderID | ImgURL |
+ --------------------------------- +
| 1132  | 1      | 1       | ...    |
| 2124  | 1      | 2       | ...    |
| 3456  | 1      | 3       | ...    |
.....................................
| N     | 1      | M       | ...    |
+ --------------------------------- +</pre></div></div>

<p>Some developers instead of using resources of mysql script language trying to solve this problem by executing numerous call, such &#8216;UPDATE table SET order_id = [value] WHERE id = [id] LIMIT 1;&#8217;. It`s wrong. MySQL language have a lot of ways to make it easy. At least UDF`s.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/02/05/updating-order-fields-in-table-with-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

