<?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>Tequila Fish &#187; MySQL</title>
	<atom:link href="http://www.tequilafish.com/category/tech/database/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tequilafish.com</link>
	<description>Ran-dumb ramblings of me...</description>
	<lastBuildDate>Thu, 26 Jan 2012 23:30:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>MySQL: Dynamic ORDER BY clause</title>
		<link>http://www.tequilafish.com/2010/08/18/mysql-dynamic-order-by-clause/</link>
		<comments>http://www.tequilafish.com/2010/08/18/mysql-dynamic-order-by-clause/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 02:03:22 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[asc]]></category>
		<category><![CDATA[desc]]></category>
		<category><![CDATA[order by]]></category>

		<guid isPermaLink="false">http://www.tequilafish.com/?p=541</guid>
		<description><![CDATA[When developing some discography software for a website, I came across the need to sort a list of releases differently depending on their release date. I wanted to sort future releases in ascending order (oldest to newest) and sort past releases in descending order (newest to oldest). This isn't an easy task, but after many [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tequilafish.com%2F2010%2F08%2F18%2Fmysql-dynamic-order-by-clause%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tequilafish.com%2F2010%2F08%2F18%2Fmysql-dynamic-order-by-clause%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>When developing some discography software for a website, I came across the need to sort a list of releases differently depending on their release date.  I wanted to sort future releases in ascending order (oldest to newest) and sort past releases in descending order (newest to oldest).  This isn't an easy task, but after many hours of trial and error, I finally figured it out with some MySQL trickery.</p>
<p>Given the following MySQL Database table named <tt>releases</tt>:</p>
<pre>
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title       | varchar(256)     | NO   |     | NULL    |                |
| releasedate | date             | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
</pre>
<p>I accomplished the dynamic <tt>ORDER BY</tt> clause by using the following query:</p>
<pre>
SELECT id,
       title,
       releasedate,
       UNIX_TIMESTAMP(releasedate) AS releasedate_unix,
       CASE WHEN releasedate > NOW() THEN 0 ELSE 1 END AS futureorpast
FROM releases
ORDER BY futureorpast ASC,
         CASE WHEN releasedate_unix > UNIX_TIMESTAMP(NOW()) THEN
            releasedate_unix
         ELSE
            (releasedate_unix * -1)
         END
</pre>
<p>Now for an explanation.  First, we need to "seperate" the results into two sets: future releases and past releases.  This is accomplished by calculating the <tt>futureorpast</tt> column upon which we can sort, so future releases are assigned a value of 0 while past releases are assigned a value of 1.  These are then sorted with future releases coming first, because obviously 0 comes before 1.</p>
<p>Next comes the tricky part: we want to sort future releases by <tt>ASC</tt> but past release by <tt>DESC</tt>.  This is unable to be accomplished using dynamic <tt>ASC</tt>/<tt>DESC</tt> in a <tt>CASE</tt> clause because you can only calculate <em>values</em> using <tt>CASE</tt>, not clauses.  The trick then is to convert the date column into a unix timestamp so that we have an integer, and then take the inverse of that integer on the values we want to sort in reverse.</p>
<p>Using this technique I was able to sort a subset of results in one direction and another subset of the same results in a different direction.  I'm not sure if this is the best way to accomplish this, so if anyone has other suggestions on how to accomplish this, I'd love to hear them!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tequilafish.com/2010/08/18/mysql-dynamic-order-by-clause/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL: Global find &amp; replace</title>
		<link>http://www.tequilafish.com/2010/04/29/mysql-global-find-replace/</link>
		<comments>http://www.tequilafish.com/2010/04/29/mysql-global-find-replace/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 02:25:05 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://www.tequilafish.com/?p=412</guid>
		<description><![CDATA[Global find &#038; replace is easy in MySQL: UPDATE table_name SET column_name = replace(column_name, "searchString", "replaceString");]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.tequilafish.com%2F2010%2F04%2F29%2Fmysql-global-find-replace%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.tequilafish.com%2F2010%2F04%2F29%2Fmysql-global-find-replace%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Global find &#038; replace is easy in MySQL:</p>
<p><code>UPDATE table_name SET column_name = replace(column_name, "searchString", "replaceString");</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tequilafish.com/2010/04/29/mysql-global-find-replace/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

