<?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>bradym.net &#187; MySQL</title>
	<atom:link href="http://bradym.net/category/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://bradym.net</link>
	<description>Random thoughts of a Code Monkey</description>
	<lastBuildDate>Sun, 11 Oct 2009 22:39:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Logging requests with MySQL</title>
		<link>http://bradym.net/mysql/logging-requests-with-mysql</link>
		<comments>http://bradym.net/mysql/logging-requests-with-mysql#comments</comments>
		<pubDate>Sat, 12 Jul 2008 22:49:33 +0000</pubDate>
		<dc:creator>bradym</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://bradym.net/?p=13</guid>
		<description><![CDATA[One of my recent work projects was publishing a widget which others can add to their websites by copying some code and using it on their site. Similar to how YouTube makes it easy to embed their videos elsewhere.
Of course we want to know how and where this widget is being used so we can [...]]]></description>
			<content:encoded><![CDATA[<p>One of my recent work projects was publishing a widget which others can add to their websites by copying some code and using it on their site. Similar to how <a href="http://youtube.com">YouTube</a> makes it easy to embed their videos elsewhere.</p>
<p>Of course we want to know how and where this widget is being used so we can decide if it&#8217;s worth doing more widgets in the future, so we needed to include tracking. What we want to know is where the widget is being used, and how many times it is being displayed on those URLs.</p>
<p>The tracking table is very simple:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> widget_impressions
<span style="color: #FF00FF;">&#40;</span>
	id <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">auto_increment</span><span style="color: #000033;">,</span>
        referral_url <span style="color: #999900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
        counter <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
        date_created <span style="color: #999900; font-weight: bold;">date</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
        <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>  <span style="color: #FF00FF;">&#40;</span>referral_url<span style="color: #000033;">,</span>date_created<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
        <span style="color: #FF9900; font-weight: bold;">UNIQUE</span> <span style="color: #990099; font-weight: bold;">KEY</span> id <span style="color: #FF00FF;">&#40;</span>id<span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span></pre></div></div>

<p>Technically, the id column is not needed since the primary key is a combination of the referral_url and the date_viewed field, but it can be handy in some situations.</p>
<p>When the widget is displayed, the following query is run:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> widget_impressions
	<span style="color: #990099; font-weight: bold;">SET</span>
		referral_url <span style="color: #CC0099;">=</span> <span style="color: #008000;">'http://example.com'</span><span style="color: #000033;">,</span>
		date_viewed <span style="color: #CC0099;">=</span> <span style="color: #000099;">now</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
		counter <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span>
	<span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #990099; font-weight: bold;">DUPLICATE KEY</span> <span style="color: #990099; font-weight: bold;">UPDATE</span>
		counter <span style="color: #CC0099;">=</span> counter <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span></pre></div></div>

<p>If this is the first visit from http://example.com for the day, a new record will be added to the table. If it&#8217;s not the first record for the day, the counter will be incremented by one. Using the date type for the date_viewed field means only the date is stored, not the time. If a datetime field were used, this method would not work the same way; we&#8217;d have separate entries in the table every time someone viewed the widget.</p>
<p>With this table structure it&#8217;s very easy to get stats. Say you want to know all of the pages where the widget is displayed and how many times it has been displayed on each:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> referral_url<span style="color: #000033;">,</span> <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>counter<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> num_views
<span style="color: #990099; font-weight: bold;">FROM</span> widget_impressions
<span style="color: #990099; font-weight: bold;">GROUP BY</span> referral_url<span style="color: #000033;">;</span></pre></div></div>

<p>Want to know the 10 sites that are displaying your widget the most?</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> referral_url<span style="color: #000033;">,</span> <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>counter<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> num_views
<span style="color: #990099; font-weight: bold;">FROM</span> widget_impressions
<span style="color: #990099; font-weight: bold;">GROUP BY</span> referral_url
<span style="color: #990099; font-weight: bold;">ORDER BY</span> num_views <span style="color: #990099; font-weight: bold;">DESC</span>
<span style="color: #990099; font-weight: bold;">LIMIT</span> <span style="color: #008080;">10</span><span style="color: #000033;">;</span></pre></div></div>

<p>References:</p>
<ul>
<li><a href="http://bradym.net/mysql/on-duplicate-key-update">MySQL &#8211; on duplicate key update</a></li>
<li><a href="http://http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html">Overview of Date and Time Types</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now">now()</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fbradym.net%2Fmysql%2Flogging-requests-with-mysql';
  addthis_title  = 'Logging+requests+with+MySQL';
  addthis_pub    = 'mydarb';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://bradym.net/mysql/logging-requests-with-mysql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL &#8211; on duplicate key update</title>
		<link>http://bradym.net/mysql/on-duplicate-key-update</link>
		<comments>http://bradym.net/mysql/on-duplicate-key-update#comments</comments>
		<pubDate>Mon, 07 Jul 2008 01:57:00 +0000</pubDate>
		<dc:creator>bradym</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://bradym.net/?p=12</guid>
		<description><![CDATA[If you&#8217;ve ever built an application that interacts with a database and allows users to edit data, or one that migrates data from one system to another you&#8217;ve probably written code like this:

// $id would have been set based on some other operation above.
// I'm setting it here for clarification.
$id = 1;
&#160;
$result = mysql_query&#40;&#34;SELECT id [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever built an application that interacts with a database and allows users to edit data, or one that migrates data from one system to another you&#8217;ve probably written code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// $id would have been set based on some other operation above.</span>
<span style="color: #666666; font-style: italic;">// I'm setting it here for clarification.</span>
<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM table_name WHERE id = <span style="color: #006699; font-weight: bold;">$id</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">// Zero results, the record doesn't exist, add it.</span>
     <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO table_name (id,a,b) VALUES (<span style="color: #006699; font-weight: bold;">$id</span>,2,3)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">// The record exists, update it.</span>
     <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE table_name SET a = 1, b = 2 WHERE id = <span style="color: #006699; font-weight: bold;">$id</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Well, thanks to the ON DUPLICATE KEY UPDATE option in MySQL this can be condensed to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO table_name SET id = <span style="color: #006699; font-weight: bold;">$id</span>, a = 2, b = 3
					 ON DUPLICATE KEY UPDATE
					 a = 2, c = 3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Notes:</p>
<ul>
<li>You don&#8217;t have to update every field on update. If you only need one row updated if the row already exists, specify only that row in the UPDATE portion of the query.</li>
<li>I prefer the above syntax (using set and writing field = value for each field) but as shown in the <a href="http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html">MySQL manual</a>, it works with the (field1,field2,field3) VALUES (value1,value2,value3) version as well.</li>
<li><em><strong>This method does not work perfectly in all situations! Be sure to test on your dev server before using in production!</strong></em></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fbradym.net%2Fmysql%2Fon-duplicate-key-update';
  addthis_title  = 'MySQL+%26%238211%3B+on+duplicate+key+update';
  addthis_pub    = 'mydarb';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://bradym.net/mysql/on-duplicate-key-update/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
