<?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>TechnoBlogy &#187; MySQL</title>
	<atom:link href="http://technoblogy.net/category/db/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://technoblogy.net</link>
	<description>Technology with a Big difference</description>
	<lastBuildDate>Thu, 09 Sep 2010 08:02:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Find week start/end given week number</title>
		<link>http://technoblogy.net/find-week-startend-given-week-number/</link>
		<comments>http://technoblogy.net/find-week-startend-given-week-number/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 14:32:43 +0000</pubDate>
		<dc:creator>Nauman</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[datediff]]></category>
		<category><![CDATA[date_add]]></category>
		<category><![CDATA[week]]></category>

		<guid isPermaLink="false">http://technoblogy.net/find-week-startend-given-week-number/</guid>
		<description><![CDATA[I have a table with some dated records. I wanted to do some weekly reports on this data. MySQL has a nifty function dubbed WEEK() which will return a week number. It allows you to break your data into week long intervals very easily. For example, the following query will tell me how many records [...]]]></description>
			<content:encoded><![CDATA[<p>I have a table with some dated records. I wanted to do some weekly reports on this data. MySQL has a nifty function dubbed <kbd>WEEK()</kbd> which will return a <em>week number</em>. It allows you to break your data into week long intervals very easily. For example, the following query will tell me how many records came in each week:</p>
<blockquote><p>SELECT COUNT(*), WEEK(mydate) </p>
<p>FROM mytable </p>
<p>GROUP BY WEEK(mydate);</p>
</blockquote>
<p>The problem here is, that the output is totally meaningless to me as I do not have a clue what does week 36 mean. What I really want is to have on the screen is a nice, human readable date interval &#8211; the beginning and ending date of any given week.</p>
<p>Surprisingly, this turns out the be a major pain in the ass. There is no simple function that will yield a week interval (or start/end date of a week) given a week number. You have to find these dates manually. Here is how I did it:</p>
<blockquote><p>SELECT COUNT(*) AS reports_in_week, DATE_ADD(mydate, INTERVAL( 1-DAYOFWEEK(mydate) ) DAY), DATE_ADD(mydate, INTERVAL( 7-DAYOFWEEK(mydate) ) DAY) </p>
<p>FROM mytable </p>
<p>GROUP BY WEEK(mydate)</p>
</blockquote>
<p>How does it work? The <kbd>DAYOFWEEK()</kbd> function returns an integer ranging from 1 (Sunday) to 7 (Saturday). So if <kbd>mydate</kbd> happens to be Tuesday we get the following statements:</p>
<blockquote><p>DATE_ADD(mydate, INTERVAL -2 DAY)</p>
</blockquote>
<p>which essentially means <em>&quot;subtract 2 days from mydate</em> (which is that week&#8217;s Sunday) and also:</p>
<blockquote><p>DATE_ADD(mydate, INTERVAL 4 DAY)</p>
</blockquote>
<p>which yields the date of that week&#8217;s Friday.</p>
<script type="text/javascript" class="owbutton" src="http://onlywire.com/btn/button_1305" title="Find week start/end given week number" url="http://technoblogy.net/find-week-startend-given-week-number/"></script>]]></content:encoded>
			<wfw:commentRss>http://technoblogy.net/find-week-startend-given-week-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MINUS query in MySQL</title>
		<link>http://technoblogy.net/minus-query-in-mysql/</link>
		<comments>http://technoblogy.net/minus-query-in-mysql/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 14:32:27 +0000</pubDate>
		<dc:creator>Nauman</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[minus]]></category>

		<guid isPermaLink="false">http://technoblogy.net/minus-query-in-mysql/</guid>
		<description><![CDATA[Mysql does not support MINUS operator which is not the end of the world. For most queries you can really care less if it&#8217;s implemented or not. My web app never needs to use it to function properly, which makes MySQL a perfect choice here. But, sometimes human beings are more demanding than simple php [...]]]></description>
			<content:encoded><![CDATA[<p>Mysql does not support MINUS operator which is not the end of the world. For most queries you can really care less if it&#8217;s implemented or not. My web app never needs to use it to function properly, which makes MySQL a perfect choice here. But, sometimes human beings are more demanding than simple php applications. Especially if you want to extract some non-obvious information from the database.</p>
<p>Just today I wanted to see who in our company does not have an assigned laptop. I have two tables &#8211; one is user table which contains names and info of all the employees. The other one is computer table which contains service tags of all the machines we own. You can join these two on username and you figure out which computer is assigned to who. But I wanted the opposite thing &#8211; which is a logical intersection of the two tables on username. Which is usually obtained via MINUS operator.</p>
<p>Of course MySQL does not have MINUS, so I spent 5 minutes staring at the screen and typing mangled SQL queries trying to extract this info. Then I decided to google for it. Here is the solution:</p>
<blockquote><p>SELECT users.username      <br />FROM users       <br />LEFT JOIN computers ON computers.username = user.username       <br />WHERE computers.username IS NULL </p>
</blockquote>
<script type="text/javascript" class="owbutton" src="http://onlywire.com/btn/button_1305" title="MINUS query in MySQL" url="http://technoblogy.net/minus-query-in-mysql/"></script>]]></content:encoded>
			<wfw:commentRss>http://technoblogy.net/minus-query-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
