<?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>Vance Lucas &#187; Database</title>
	<atom:link href="http://www.vancelucas.com/blog/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vancelucas.com</link>
	<description>Web Entrepreneur and Freelance PHP/Javascript Developer</description>
	<lastBuildDate>Thu, 17 Nov 2011 15:30:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL Error: 1033 Incorrect information in file</title>
		<link>http://www.vancelucas.com/blog/mysql-error-1033-incorrect-information-in-file/</link>
		<comments>http://www.vancelucas.com/blog/mysql-error-1033-incorrect-information-in-file/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 20:52:27 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysql errors]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=705</guid>
		<description><![CDATA[I recently encountered this error on Disposeamail &#8211; a free disposable email site of mine that uses MySQL heavily for storing all incoming mail through an email pipe script. I did a lot of researching, and basically, there are a few primary culprits I was able to identify that will hopefully save you some time. Check your [...]]]></description>
			<content:encoded><![CDATA[<p>I recently encountered this error on <a href="http://disposeamail.com">Disposeamail</a> &#8211; a free disposable email site of mine that uses MySQL heavily for storing all incoming mail through an email pipe script.</p>
<p>I did a lot of researching, and basically, there are a few primary culprits I was able to identify that will hopefully save you some time.</p>
<p><span id="more-705"></span></p>
<h3>Check your /tmp directory</h3>
<p>MySQL will produce this error sometimes when the temp directory is not writeable.</p>
<ol>
<li>Ensure that <strong>/tmp</strong> (and/or /var/tmp) has the <strong>correct permissions</strong> (777)</li>
<li>Check the <strong>my.cnf </strong>file and search for a <strong>tmpdir</strong>=/tmp flag. Ensure the value is pointing to the correct temp directory.</li>
<li>Ensure your <strong>/tmp directory is not full</strong></li>
</ol>
<h3>Check your my.cnf</h3>
<ol>
<li>If you <strong>made changes</strong> recently, <strong>revert them</strong> and restart MySQL (especially InnoDB Buffer Pool settings)</li>
<li><strong>Restore my.cnf.back</strong> is there is one</li>
<li>If you are using <strong>InnoDB tables</strong>, ensure the <strong>skip-innodb</strong> line in my.cnf is <strong>commented out</strong> or removed.</li>
</ol>
<h3>Clear InnoDB Log Files</h3>
<p>This step <strong>ONLY APPLIES IF THE ABOVE STEPS DID NOT WORK</strong>.</p>
<p>Read the <a href="http://dev.mysql.com/doc/refman/5.0/en/innodb-data-log-reconfiguration.html">MySQL Manual page on removing InnoDB log files</a> for a safer backup and restoration procedures. Basically, the steps are:</p>
<ol>
<li>Shut down MySQL</li>
<li><strong>Remove ib_logfile*</strong> files from the MySQL data directory (move them or rename them if you want to be safe)</li>
<li>Re-start MySQL</li>
</ol>
<p>My specific problem was that somehow the &#8220;skip-innodb&#8221; line got added back into my &#8220;my.cnf&#8221; file, so MySQL was expecting a different table format when loading data. I suspect this had something to do with my cPanel/WHM setup overwriting the file, but I&#8217;ll never know for sure.</p>
<p>Good Luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/mysql-error-1033-incorrect-information-in-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MongoDB Gotchas</title>
		<link>http://www.vancelucas.com/blog/mongodb-gotchas/</link>
		<comments>http://www.vancelucas.com/blog/mongodb-gotchas/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 17:37:10 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[mongodb]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=562</guid>
		<description><![CDATA[Most developers are coming from a background with relational database-specific experience, and then trying out some new NoSQL databases like MongoDB. Here are some &#8220;gotchas&#8221; I ran into while using MongoDB with my MySQL hat still on. Queries are case-sensitive Fields and queries in MongoDB are case-sensitive: 1 2 3 var test1 = db.test.find&#40;&#123;'tags': 'jquery'&#125;&#41;.count&#40;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Most developers are coming from a background with relational database-specific experience, and then trying out some new NoSQL databases like <a href="http://mongodb.org">MongoDB</a>. Here are some &#8220;gotchas&#8221; I ran into while using MongoDB with my MySQL hat still on.<span id="more-562"></span></p>
<h3>Queries are case-sensitive</h3>
<p>Fields and queries in MongoDB are case-sensitive:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> test1 <span style="color: #339933;">=</span> db.<span style="color: #660066;">test</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'tags'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'jquery'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> test2 <span style="color: #339933;">=</span> db.<span style="color: #660066;">test</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'tags'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'jQuery'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
test1 <span style="color: #339933;">==</span> test2<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Output is false - they do not query for the same information</span></pre></td></tr></table></div>

<p>This can cause some headaches if you don&#8217;t normalize user input ahead of time. Imagine you already have several posts tagged with &#8216;NoSQL&#8217; and users enter &#8216;nosql&#8217; in a tag search box. If you don&#8217;t normalize how the data is stored internally (like lowercase all tags), your user will see a much smaller set of posts than they are expecting to see. This is something you don&#8217;t have to worry or even think about with MySQL and most other relational databases.</p>
<p>If you can&#8217;t normalize the stored data, but still want a case-insensitive query, then you must perform a slower <a href="http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions">regular expression query</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">db.<span style="color: #660066;">test</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'tags'</span><span style="color: #339933;">:</span> <span style="color: #339933;">/</span>jquery<span style="color: #339933;">/</span>i<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Note the 'i' flag for case-insensitive</span></pre></td></tr></table></div>

<h3>Data is type-sensitive</h3>
<p>Data stored within MongoDB knows it&#8217;s type. There is a small but significant difference between these two records:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">102</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 'count' is stored as an int</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;102&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 'count' is stored as a string</span></pre></td></tr></table></div>

<p>This is obvious to any programmer when presented like this, but what may not be obvious is that this also affects how you can query for these records:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// This returns 1 instead of 2, because it only matches the integer value</span>
db.<span style="color: #660066;">test</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">102</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is due to how MongoDB stores documents internally with <a href="http://bsonspec.org/">BSON</a> (Binary JSON), using various <a href="http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions">MongoDB data types</a>. This means &#8211; like the point above &#8211; that you must pay attention to how you are saving data <em>into</em> MongoDB, because it will affect how you can query for it later.</p>
<h3>Documents sizes are capped at 4MB each</h3>
<p>This isn&#8217;t a big issue for most people, but it&#8217;s something to be aware of if you plan on storing large chunks of text or nesting a bunch of objects inside a single document. <a href="http://www.businessinsider.com/how-we-use-mongodb-2009-11">Nesting comments inside article documents</a> is a particular approach that may give you pause knowing there is an upper threshold on document size.</p>
<p>Note that this limitation is not an issue for storing <em>files</em> in MongoDB because <a href="http://www.mongodb.org/display/DOCS/GridFS">GridFS</a> transparently divides the contents of files larger than 4MB across multiple documents.</p>
<h3>Only one index is used per query</h3>
<p>Simply adding more indexes doesn&#8217;t always help queries run faster. MongoDB can&#8217;t use multiple indexes together like MySQL and other RDBMS can (see &#8220;<a href="http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html">Index Merge Optimization</a>&#8220;). This means that if a query is selecting or sorting based on multiple fields, a compound index should be created with those fields for the query to run most efficiently.</p>
<h3>MongoDB is for high-memory and 64-bit systems only</h3>
<p>Although MongoDB can be downloaded, compiled, and installed on 32-bit systems, it should never be run in production on them. This is because <a href="http://www.mongodb.org/display/DOCS/Indexing+Advice+and+FAQ#IndexingAdviceandFAQ-MakesureyourindexescanfitinRAM.">MongoDB stores all indexes in memory</a> as well as <a href="http://www.mongodb.org/display/DOCS/Caching">memory-mapped files</a> for all disk I/O for increased speed and throughput. While these two facts aren&#8217;t &#8220;gotchas&#8221; themselves &#8211; they are clearly explained on the website &#8211; it does mean that MongoDB can quickly use a lot of memory, especially as the size of your database grows and becomes significant. Since 32-bit systems have an effective <a href="http://en.wikipedia.org/wiki/3_GB_barrier">3GB memory limit</a> of addressable memory space, they prevent you from being able to add more memory as the size of your database grows. This is also something to think about if you plan to run MongoDB on a small VPS with limited memory, even if it is 64-bit. You may be forced into a memory upgrade sooner than you are prepared for if your database is large or rapidly growing.</p>
<h3>Subtle Differences</h3>
<p>When starting out with MongoDB, it&#8217;s easy to draw a lot of parallels to MySQL and make a lot of assumptions based on those similarities. Collections are kind of like tables, fields are kind of like columns, a document is kind of like a row. You may find yourself going on to make more assumptions about how it&#8217;s storing data, how you can query it, what you can do with it, etc. without even knowing it. So while MongoDB has a lot of similar features and is one of the easiest NoSQL databases to transition to from a relational database, it has some very distinct differences &#8220;under the hood&#8221; that you have to be aware of and plan for ahead of time. The <a href="http://www.mongodb.org/display/DOCS/Manual">MongoDB documentation</a> is well-written, and is pretty good at explaining any potential differences or &#8220;gotchas&#8221;, so make sure to read it thoroughly before making the jump &#8211; and watch that first step.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/mongodb-gotchas/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>NoSQL First Impressions: Object Databases Missed the Boat</title>
		<link>http://www.vancelucas.com/blog/nosql-first-impressions-object-databases-missed-the-boat/</link>
		<comments>http://www.vancelucas.com/blog/nosql-first-impressions-object-databases-missed-the-boat/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 17:00:57 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[rdbms oodb]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=551</guid>
		<description><![CDATA[I&#8217;ve spent the past few weeks here at work researching and playing with NoSQL databases (and especially MongoDB) for a new feature we&#8217;re developing that doesn&#8217;t easily fit into a relational model. And so far, I really like what I see. The profoundness of the shift away from the relational model and the implications that [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent the past few weeks here at work researching and playing with <a href="http://en.wikipedia.org/wiki/NoSQL">NoSQL</a> databases (and especially <a href="http://mongodb.org">MongoDB</a>) for a new feature we&#8217;re developing that doesn&#8217;t easily fit into a relational model. And so far, I <em>really</em> like what I see. The profoundness of the shift away from the relational model and the implications that has just blow my mind. <strong>You no longer have to fragment your data to persist it</strong><em>.</em> You just store it. That&#8217;s it. No more hours toiling over the design your table schema and how to break apart the data you put together just to fit it into a relational model. You can now store your data exactly how you use it in your application, with no other special needs or <a href="http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch">impedance mismatches</a>. Going back to an <a href="http://en.wikipedia.org/wiki/RDBMS">RDBMS</a> system now just seems illogical &#8211; it&#8217;s like breaking apart a camera tripod just to fit it in the same standard size case you&#8217;ve been using for years instead of just collapsing it and finding a different case that fits it better. All that effort you go though tearing down your tripod and putting it back together every time you use it is wasted and unnecessary. It&#8217;s a symptom of the larger problem that your case doesn&#8217;t fit your tripod.<span id="more-551"></span></p>
<p>Once you fully get the NoSQL viewpoint and the implications it has on  the future of data storage, you have one of those &#8220;why didn&#8217;t anyone  think of this sooner?&#8221; moments. And yes, I know key/value stores like <a href="http://memcached.org/">memcached</a> existed long before this whole new &#8220;NoSQL&#8221; movement, but it&#8217;s not quite  the same thing.</p>
<h3>Object Databases</h3>
<p>Object databases are an interesting case. They are in the &#8220;NoSQL&#8221; category and solve the same problem other NoSQL databases do &#8211; creating a storage mechanism that fits your data instead of making your data fit a predefined rigid storage mechanism. So why didn&#8217;t they catch on? What gives? It&#8217;s anyone&#8217;s guess why object databases didn&#8217;t catch on and achieve widespread adoption, but one thing is for sure &#8211; <strong>object databases really missed the boat</strong> while other NoSQL database technologies are running rings around them.</p>
<p>The primary problem is that most object databases are designed for a specific language, like Java or .NET. Some have more language support than others, but the problem is still the same &#8211; by integrating in directly with a language&#8217;s object model they depend on specific language implementations, which severely limits adoption. They solve the impedance mismatch problem that RDBMSes present, but <strong>fail to provide language-agnostic data persistence and access</strong>, effectively tying their use to specific technology stacks and platforms.</p>
<h3>The Best of Both Worlds</h3>
<p>NoSQL databases are gaining traction like no other database technology has in over 30 years. The reason is that <strong>good NoSQL databases solve both key data storage problems: impedance mismatches AND language-agnosticism.</strong> By storing the data in an intermediary format like JSON (or BSON) and allowing custom file content types, the focus is on the actual data itself instead of specific technologies or data models. The technology stack becomes irrelevant, and the walls to gaining user adoption crumble.</p>
<p>The bottom line is that NoSQL databases are not just the latest fad. They represent a fundamental paradigm shift in data storage and are an entirely new way of thinking about how to store and access your data in a way that makes sense for your actual usage. NoSQL databases are absolutely something you should be paying attention to and following closely &#8211; they are a strong contender to the RDBMS model and will likely become the de-facto data storage choice for most new web applications within a decade.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/nosql-first-impressions-object-databases-missed-the-boat/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>MySQL Series: How to Detect UTF-8 and Multi-byte Characters</title>
		<link>http://www.vancelucas.com/blog/mysql-series-how-to-detect-utf-8-and-multi-byte-characters/</link>
		<comments>http://www.vancelucas.com/blog/mysql-series-how-to-detect-utf-8-and-multi-byte-characters/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 15:44:39 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=542</guid>
		<description><![CDATA[Multi-byte characters can cause quite a few headaches for the unsuspecting webmaster. Sometimes all you need to do to figure out how to fix the problem is detect which database records have UTF-8 data in them and which ones do not. If you&#8217;ve been scanning records manually, stop now. Here&#8217;s a quick query to cure [...]]]></description>
			<content:encoded><![CDATA[<p>Multi-byte characters can cause quite a few headaches for the unsuspecting webmaster. Sometimes all you need to do to figure out how to fix the problem is detect which database records have UTF-8 data in them and which ones do not. If you&#8217;ve been scanning records manually, stop now. Here&#8217;s a quick query to cure your ales:</p>
<p>Return all the rows with multi-byte characters in table <em>posts</em> on field <em>title</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="sql" 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> posts
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>title<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #993333; font-weight: bold;">CHAR_LENGTH</span><span style="color: #66cc66;">&#40;</span>title<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This does pretty much what it looks like: returns all the rows in the posts table where the title&#8217;s character length does not match the title&#8217;s length. This works because the LENGTH function returns the <em>number of bytes</em> in the string, while the CHAR_LENGTH function returns the <em>number of characters</em> in the string. If the string contains multi-byte characters (individual characters that are made up of more than one byte) like international UTF-8 characters, the two functions will return different numbers and will not be equal, thus including that row in your results.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/mysql-series-how-to-detect-utf-8-and-multi-byte-characters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Series: Return NULL Values First With Descending Order</title>
		<link>http://www.vancelucas.com/blog/mysql-series-return-null-values-first-with-descending-order/</link>
		<comments>http://www.vancelucas.com/blog/mysql-series-return-null-values-first-with-descending-order/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 21:28:55 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=486</guid>
		<description><![CDATA[Sometimes there are unique situations where you need to order query results by a particular field in descending order, but also need NULL values first. The default (and logical) behavior of MySQL in this case is to return NULL values last, because in descending order they have the lowest value (none). But what if you [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes there are unique situations where you need to order query results by a particular field in descending order, but also need NULL values first. The default (and logical) behavior of MySQL in this case is to return NULL values last, because in descending order they have the lowest value (none). But what if you really need to reverse this and force NULL values to the top of the result set?<br />
<span id="more-486"></span><br />
I recently ran into this situation with movie results. The business requirement was that the movies be displayed in descending order by release date, so that the newest ones appeared first. Okay, pretty simple &#8211; until I looked at the results. Turns out, we had data on movies that were currently in production, but did not yet have a release date set. I accounted for this earlier by allowing the release date column to be NULL in the table schema, but forgot about the ramifications of that on my queries. These movies were obviously newer, but were always displayed last on the results page because of their NULL release dates. Sounds like the perfect scenario for a conditional statement.</p>
<p>Using the <a href="http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html">MySQL Control Flow Functions</a>, I quickly crafted an IF() statement in the SELECT portion of the query that looked like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #009900;">IF</span><span style="color: #FF00FF;">&#40;</span>m.date_released <span style="color: #CC0099; font-weight: bold;">IS</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #CC0099; font-weight: bold;">OR</span> m.date_released <span style="color: #CC0099;">=</span> <span style="color: #008000;">'0000-00-00'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> in_production</pre></td></tr></table></div>

<p>So if the release date has a NULL value or has been inserted as a blank string and formatted to &#8217;0000-00-00&#8242;, it is given a value of 1, and 0 otherwise. We then sort descending by our new &#8216;in_production&#8217; alias in the ORDER BY clause to get the NULL or empty values on top (assigned a 1 value), and then by release date and other criteria second.</p>
<p>The whole query looks something like this:</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> m.<span style="color: #CC0099;">*</span><span style="color: #000033;">,</span> <span style="color: #009900;">IF</span><span style="color: #FF00FF;">&#40;</span>m.date_released <span style="color: #CC0099; font-weight: bold;">IS</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #CC0099; font-weight: bold;">OR</span> m.date_released <span style="color: #CC0099;">=</span> <span style="color: #008000;">'0000-00-00'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> in_production
<span style="color: #990099; font-weight: bold;">FROM</span> movies <span style="color: #990099; font-weight: bold;">AS</span> m
<span style="color: #990099; font-weight: bold;">ORDER BY</span> in_production <span style="color: #990099; font-weight: bold;">DESC</span><span style="color: #000033;">,</span> m.date_released <span style="color: #990099; font-weight: bold;">DESC</span></pre></td></tr></table></div>

<p>It&#8217;s an easy way to manipulate the result set in MySQL without slowing down the query or issuing multiple queries.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/mysql-series-return-null-values-first-with-descending-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

