<?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>Random Snippets &#187; query</title>
	<atom:link href="http://www.randomsnippets.com/tag/query/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to randomly order or select rows in a MySQL query</title>
		<link>http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/</link>
		<comments>http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 06:04:46 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[LIMIT]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[ORDER]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[RAND]]></category>
		<category><![CDATA[random selection]]></category>
		<category><![CDATA[SELECT]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=188</guid>
		<description><![CDATA[I was looking for a way to order the rows randomly in a MySQL query and the solution was not easy for me to come by.  I am posting the solution here in hopes of helping others.

1
2
3
SELECT * 
FROM my_table
ORDER BY RAND&#40;&#41;

RAND() returns a random floating-point value but functions to randomly order the selection [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking for a way to order the rows randomly in a MySQL query and the solution was not easy for me to come by.  I am posting the solution here in hopes of helping others.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="mysql mysql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> 
<span style="color: #993333; font-weight: bold;">FROM</span> my_table
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">RAND</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p><strong>RAND()</strong> returns a random floating-point value but functions to randomly order the selection of rows in the above usage.</p>
<p>If you combine the query with <strong>LIMIT</strong>, you will end up with a random selection of rows from your table.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="mysql mysql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> 
<span style="color: #993333; font-weight: bold;">FROM</span> my_table
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">RAND</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">3</span></pre></td></tr></table></div>

<p>Assuming you have more than 3 rows in your table, you will always get 3 random rows from the query.</p>

<script language="javascript" src="http://www.randomsnippets.com/wp-content/plugins/emailr/emailr.js"></script><script src="http://www.randomsnippets.com/wp-includes/js/jquery/jquery.js"></script>Share with a friend:<br /><input id="emailrRecipient" type="text" value="email address" onclick="this.value='';jQuery('#emailrResponse').html('');" style="color: #666;">&nbsp;&nbsp;<input id="emailrSendBtn" type="button" value="Send" onclick="this.disabled='true';sendEmail();jQuery('#emailrResponse').html('<image src=\'http://www.randomsnippets.com/wp-content/plugins/emailr/busy.gif\' />');">&nbsp;&nbsp;<div id="emailrResponse" style="display:inline;"></div><br />
<div style="clear: both;"></div>
<a id="customMsgLink" href="javascript:void(0);" onclick="jQuery('#customMessage').css('display','inline');jQuery('#customMsgLink').css('display',  'none');">Customize message</a><br />
<div id="customMessage" style="display: none;">
<input id="sender" type="text" value="your name" onclick="this.value='';" style="color: #666;">
<div style="clear: both;"></div>
<textarea rows="4" style="height: 100px;" id="emailrMessage" style="color: #666;">I think you might be interested in this: 

http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/
</textarea>
</div>
<div style="clear: both;"></div><br /><br />
<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F&amp;title=How+to+randomly+order+or+select+rows+in+a+MySQL+query" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F&amp;title=How+to+randomly+order+or+select+rows+in+a+MySQL+query" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F&amp;title=How+to+randomly+order+or+select+rows+in+a+MySQL+query" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F&amp;title=How+to+randomly+order+or+select+rows+in+a+MySQL+query" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F&amp;title=How+to+randomly+order+or+select+rows+in+a+MySQL+query', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F&amp;title=How+to+randomly+order+or+select+rows+in+a+MySQL+query" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.randomsnippets.com%2F2008%2F10%2F28%2Fhow-to-randomly-order-or-select-rows-in-a-mysql-query%2F&amp;title=How+to+randomly+order+or+select+rows+in+a+MySQL+query" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to convert MySQL timestamp to PHP date type</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/</link>
		<comments>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 05:01:52 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[date function]]></category>
		<category><![CDATA[hh mm ss]]></category>
		<category><![CDATA[mysql timestamp]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[php date]]></category>
		<category><![CDATA[php strtotime]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[unix timestamp]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=131</guid>
		<description><![CDATA[Let&#8217;s say you have the following PHP code that extracts the date from the times table in your MySQL database.  The date is of timestamp type which has the following format: &#8216;YYYY-MM-DD HH:MM:SS&#8217; or &#8216;2008-10-05 21:34:02.&#8217;

1
2
3
4
$res = mysql_query&#40;&#34;SELECT date FROM times;&#34;&#41;;
while &#40; $row = mysql_fetch_array&#40;$res&#41; &#41; &#123;
   echo $row&#91;'date'&#93; . &#34;&#60;br /&#62;&#34;;
&#125;


This [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have the following PHP code that extracts the <strong>date</strong> from the <strong>times</strong> table in your MySQL database.  The <strong>date</strong> is of <strong>timestamp</strong> type which has the following format: &#8216;YYYY-MM-DD HH:MM:SS&#8217; or &#8216;2008-10-05 21:34:02.&#8217;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$res</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 date FROM times;&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="">'date'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-131"></span><br />
This date format that is in the output is in the timestamp format, <code>2008-10-05 21:34:02</code>, which is not surprising, but you want something that is more &#8220;user-friendly&#8221; or &#8220;readable&#8221; as in <strong>&#8220;9:34 pm October 5, 2008.&#8221;</strong></p>
<p>Let&#8217;s go back to the drawing board and try again:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$res</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 date FROM times;&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;g:i a F j, Y &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A sample output from this PHP code would be <code>9:34 pm October 5, 2008</code> which is much more user-friendly.</p>
<p>The PHP <strong>strtotime</strong> function parses the MySQL timestamp into a Unix timestamp which can be utilized for further parsing or formatting in the PHP <strong>date</strong> function.</p>
<p>Here are some other sample date output formats that may be of practical use:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y g:i a&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;                  <span style="color: #666666; font-style: italic;">// October 5, 2008 9:34 pm</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;m.d.y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;                         <span style="color: #666666; font-style: italic;">// 10.05.08</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;j, n, Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;                       <span style="color: #666666; font-style: italic;">// 5, 10, 2008</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ymd&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;                           <span style="color: #666666; font-style: italic;">// 20081005</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="">'\i\t \i\s \t\h\e jS \d\a\y.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;   <span style="color: #666666; font-style: italic;">// It is the 5th day.</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D M j G:i:s T Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;               <span style="color: #666666; font-style: italic;">// Sun Oct 5 21:34:02 PST 2008</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to count values with MySQL queries</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/</link>
		<comments>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 03:40:35 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[alias]]></category>
		<category><![CDATA[column values]]></category>
		<category><![CDATA[count values]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[loop through]]></category>
		<category><![CDATA[mysql queries]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[value]]></category>
		<category><![CDATA[vote counters]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116</guid>
		<description><![CDATA[Let&#8217;s say you have the following table called votes that keeps track of how people voted and you want a query to count the number of votes for you instead of having to loop through all the rows with a counter in PHP.  


person
vote


obama
yes


mccain
no


obama
yes


obama
no


mccain
yes


obama
yes


obama
yes


obama
no


mccain
no



Here is the MySQL query that would do just the job.

SELECT [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have the following table called <strong>votes</strong> that keeps track of how people voted and you want a query to count the number of votes for you instead of having to loop through all the rows with a counter in PHP.  </p>
<table>
<tr>
<th>person</th>
<th>vote</th>
</tr>
<tr>
<td>obama</td>
<td>yes</td>
</tr>
<tr>
<td>mccain</td>
<td>no</td>
</tr>
<tr>
<td>obama</td>
<td>yes</td>
</tr>
<tr>
<td>obama</td>
<td>no</td>
</tr>
<tr>
<td>mccain</td>
<td>yes</td>
</tr>
<tr>
<td>obama</td>
<td>yes</td>
</tr>
<tr>
<td>obama</td>
<td>yes</td>
</tr>
<tr>
<td>obama</td>
<td>no</td>
</tr>
<tr>
<td>mccain</td>
<td>no</td>
</tr>
</table>
<p><span id="more-116"></span></p>
<p>Here is the MySQL query that would do just the job.<br />
<code><br />
SELECT person,<br />
SUM(IF(vote = "yes", 1,0)) AS `yes_votes`,<br />
SUM(IF(vote = "no", 1,0)) AS `no_votes`,<br />
COUNT(vote) AS `total`<br />
FROM votes<br />
GROUP BY person<br />
ORDER BY yes_votes DESC<br />
</code></p>
<p>The following would be the result of the query:</p>
<table>
<tr>
<th>person</th>
<th>yes_votes</th>
<th>no_votes</th>
<th>total</th>
</tr>
<tr>
<td>obama</td>
<td>4</td>
<td>2</td>
<td>6</td>
</tr>
<tr>
<td>mccain</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
<p>The beauty of the query all lies in the <strong>SUM IF</strong> statements.  For example, in order to count the <strong>yes</strong> votes, the IF statement will check to see if <strong>vote = &#8220;yes&#8221;</strong> as it loops through all the rows.  If so, the <strong>yes_votes</strong> column alias is incremented by <strong>1</strong>.  The same procedure goes for counting the no votes.  The <strong>COUNT</strong> statements keeps a tally on the total number of votes.  The <strong>GROUP BY person</strong> statement allows the vote counters to calculate the numbers by person instead of the total number of yes and no votes.  </p>
<p>Now, you do not have to loop through your MySQL results to count column values =)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
