<?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>tech stuff &#187; mysql query</title>
	<atom:link href="http://codytaylor.org/tag/mysql-query/feed" rel="self" type="application/rss+xml" />
	<link>http://codytaylor.org</link>
	<description>From Cody Taylor.</description>
	<lastBuildDate>Sun, 30 Oct 2011 04:15:41 +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>MySQL SUM() Doesn&#8217;t Play Well With Floats</title>
		<link>http://codytaylor.org/2009/10/mysql-sum-doesnt-play-well-with-floats.html</link>
		<comments>http://codytaylor.org/2009/10/mysql-sum-doesnt-play-well-with-floats.html#comments</comments>
		<pubDate>Wed, 28 Oct 2009 00:43:11 +0000</pubDate>
		<dc:creator>Cody Taylor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[problems and solutions]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[round]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sum]]></category>

		<guid isPermaLink="false">http://codytaylor.org/?p=14302</guid>
		<description><![CDATA[Apparently floats and doubles use floating point math, which deals with approximate values for numbers and can thus result in confusion like this. It seems that it isn't really possible to store 0.1 in a column of type float. You can only store 0.00999999977648258.]]></description>
			<content:encoded><![CDATA[<p>I had to write some reports for some legacy software today and I was unpleasantly surprised with the results of my SQL queries. I was selecting dollar values and summing them to for the monthly spending of certain individuals. Easy enough right? I wrote a query something like this :<br />
<pre><code>
SELECT SUM(t.money_spent) as sum_of_spent,
c.customer_name 
from transactions t 
join customers c on t.customer_id=c.customer_id 
group by customer_name order by c.customer_name asc
</code></pre><br />
I ended up getting numerical values that were 10 decimal places long with seemingly random numbers. After checking to make sure the database didn&#8217;t have any odd entries I stumbled on this <a href='http://bugs.mysql.com/bug.php?id=1961' title='bug report'>bug report</a>.<br />
The &#8216;money_spent&#8217; column had a data type of float, which is a waste, but I still don&#8217;t think that it should sum up incorrectly. When I select individual values I get proper two decimal results.<br />
Apparently floats and doubles use floating point math, which deals with approximate values for numbers and can thus result in confusion like this. It seems that it isn&#8217;t really possible to store 0.1 in a column of type float. You can only store 0.00999999977648258. This behavior is a little silly but easily fixed by using the ROUND() function :<br />
<pre><code>
SELECT SUM(ROUND(t.money_spent)) as sum_of_money_spent,
c.customer_name from transactions t 
join customers c on t.customer_id=c.customer_id 
group by customer_name order by c.customer_name asc
</code></pre></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodytaylor.org%2F2009%2F10%2Fmysql-sum-doesnt-play-well-with-floats.html&amp;linkname=MySQL%20SUM%28%29%20Doesn%26%238217%3Bt%20Play%20Well%20With%20Floats"><img src="http://codytaylor.org/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codytaylor.org/2009/10/mysql-sum-doesnt-play-well-with-floats.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Search the wordpress content management system database</title>
		<link>http://codytaylor.org/2009/10/search-the-wordpress-content-management-system-database.html</link>
		<comments>http://codytaylor.org/2009/10/search-the-wordpress-content-management-system-database.html#comments</comments>
		<pubDate>Thu, 22 Oct 2009 01:29:36 +0000</pubDate>
		<dc:creator>Cody Taylor</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[content management system]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[wordpress database]]></category>

		<guid isPermaLink="false">http://codytaylor.org/?p=14299</guid>
		<description><![CDATA[SQL queries to search and replace posts from a wordpress mysql database.]]></description>
			<content:encoded><![CDATA[<p>Wordpress is by far the most popular content management system for blog hosting. The wordpress content management system uses the mysql database. If you have a big site with a large number of posts then it can be handy to search the content of every post to find certain text. Sometimes you may even need to replace certain keywords with other keywords. As with most content management setups there is probably a plugin that will do just that, but it is far easier to just use basic sql if you know the structure of the wordpress database.<br />
Within either phpmyadmin or mysqlyog (depending on what you are using) you can use this sql query to find the text that you are looking for:<br />
<pre><code>
select * from wp_posts where post_content 
like &#039;%content management system%&#039;;
</code></pre><br />
The ID that you get back is basically the page id. For example, if I query my database and get back an id of 13449 then that content will reside at http://codytaylor.org/?p=13449. Other useful columns are the post_content which is the content text of the post, post_name which is the title of the post, and the guid which is the full url (before mod_rewrite changes it) so you don&#8217;t have to copy and paste the id and append it to your url.</p>
<p>If you need to search and replace some text in more than one post then you can use this sql :<br />
<pre><code>
UPDATE wp_posts SET post_content = REPLACE (
post_content, &#039;content management system&#039;, &#039;CMS&#039;);
</code></pre><br />
That SQL query will replace the &#8216;content management system&#8217; with &#8216;CMS&#8217;.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodytaylor.org%2F2009%2F10%2Fsearch-the-wordpress-content-management-system-database.html&amp;linkname=Search%20the%20wordpress%20content%20management%20system%20database"><img src="http://codytaylor.org/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codytaylor.org/2009/10/search-the-wordpress-content-management-system-database.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

