<?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; mysql query</title>
	<atom:link href="http://www.randomsnippets.com/tag/mysql-query/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code for Web Developers</description>
	<lastBuildDate>Fri, 25 Nov 2011 23:28:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<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>Allen Liu</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 &#8230; <a href="http://www.randomsnippets.com/2008/10/28/how-to-randomly-order-or-select-rows-in-a-mysql-query/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> 
<span style="color: #990099; font-weight: bold;">FROM</span> my_table
<span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #000099;">RAND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#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" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> 
<span style="color: #990099; font-weight: bold;">FROM</span> my_table
<span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #000099;">RAND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">LIMIT</span> <span style="color: #008080;">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>
]]></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>1</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>Allen Liu</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 &#8230; <a href="http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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>22</slash:comments>
		</item>
	</channel>
</rss>

