<?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>Otaqui.Com &#187; mysql</title>
	<atom:link href="http://otaqui.com/blog/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://otaqui.com/blog</link>
	<description>Pete Otaqui's blog about web development and everything else</description>
	<lastBuildDate>Wed, 23 Jun 2010 09:55:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Selecting a Different Table Column if the Original Record is NULL in MYSQL Using IFNULL</title>
		<link>http://otaqui.com/blog/398/selecting-a-different-table-column-if-the-original-record-is-null-in-mysql-using-ifnull/</link>
		<comments>http://otaqui.com/blog/398/selecting-a-different-table-column-if-the-original-record-is-null-in-mysql-using-ifnull/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 13:31:13 +0000</pubDate>
		<dc:creator>pete</dc:creator>
				<category><![CDATA[Professional]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://otaqui.com/blog/?p=398</guid>
		<description><![CDATA[MySQL&#8217;s documentation is OK, but it&#8217;s examples are sometimes quite poor. I have a particular setup where values across two tables &#8211; a course table and a schedule table which relates to it &#8211; can be effectively &#8220;overridden&#8221;. The idea is that for any given course, one can set &#8220;default&#8221; values, and then these can [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL&#8217;s documentation is OK, but it&#8217;s examples are sometimes quite poor.</p>
<p>I have a particular setup where values across two tables &#8211; a course table and a schedule table which relates to it &#8211; can be effectively &#8220;overridden&#8221;.  The idea is that for any given course, one can set &#8220;default&#8221; values, and then these can be overridden every time a course is actually scheduled to happen.</p>
<p>The courses table looks something like this:</p>
<p>COURSES<br />
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY<br />
title VARCHAR(255) NOT NULL<br />
subtitle VARCHAR(255) NOT NULL<br />
description TEXT NOT NULL</p>
<p>SCHEDULES<br />
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY<br />
course_id INT(11) NOT NULL<br />
subtitle VARCHAR(255) NULL<br />
description TEXT NULL</p>
<p>Hopefully you can see what I would want during a SELECT &#8211; to get the values from the schedules table if they aren&#8217;t null, or otherwise get the value from the courses table if they are.</p>
<p>This is achieved with the IFNULL built in function in MySQL.  IFNULL takes two arguments, and returns the first argument if it (the first argument that is) is not null, or the second argument if it is.  The actual SQL for my example looks like this:</p>
<p>SELECT courses.title, IFNULL(schedules.subtitle,courses.subtitle) AS subtitle, IFNULL(schedules.description,courses.description) AS description FROM schedules LEFT JOIN courses ON schedule.course_id=course.id;</p>
<p>This will give you the &#8220;subtitle&#8221; and &#8220;description&#8221; values from the linked table (schedules) if they exist, or the default values from the original table (courses) if they don&#8217;t.</p>
]]></content:encoded>
			<wfw:commentRss>http://otaqui.com/blog/398/selecting-a-different-table-column-if-the-original-record-is-null-in-mysql-using-ifnull/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Performing Operations on MySQL Data While Copying between Tables</title>
		<link>http://otaqui.com/blog/396/performing-operations-on-mysql-data-while-copying-between-tables/</link>
		<comments>http://otaqui.com/blog/396/performing-operations-on-mysql-data-while-copying-between-tables/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 17:11:06 +0000</pubDate>
		<dc:creator>pete</dc:creator>
				<category><![CDATA[Professional]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://otaqui.com/blog/?p=396</guid>
		<description><![CDATA[I had the need to copy a load of data from one table to another while incrementing (or just adding an arbitrary number to) some numeric indices. Before I lept to a programming language like PHP to do a load of selects, manipulate the data and then run inserts I thought I&#8217;d try and do [...]]]></description>
			<content:encoded><![CDATA[<p>I had the need to copy a load of data from one table to another while incrementing (or just adding an arbitrary number to) some numeric indices.</p>
<p>Before I lept to a programming language like PHP to do a load of selects, manipulate the data and then run inserts I thought I&#8217;d try and do it in pure SQL.  It turns out this is fairly straightforward.</p>
<p>Let&#8217;s assume we have two databases, db_source and db_target and that we want to copy stuff from the former into the latter.  Both of these have identical tables called &#8216;users&#8217; and &#8216;posts&#8217; like so:</p>
<p>USERS<br />
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,<br />
name VARCHAR(255) NOT NULL</p>
<p>POSTS<br />
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,<br />
user_id INT(11) NOT NULL,<br />
title VARCHAR(255),<br />
post TEXT</p>
<p>Now, let&#8217;s assume that in db_target we *already* have some posts and some users, and what we really want to do is merge the records from db_source with the ones that already in db_target, obviously maintaining all the related keys.</p>
<p>In this example we will say that we have 5 current users in db_target, and 100 posts; and also that the auto_increment values are 1 to 1 in both tables (that is the next auto_increment value will be 6 and 101 for users and posts respectively in db_target).</p>
<p>With these two commands, which will run SO much faster than some kind of programmatic loop, we can copy all the users and posts at a stroke without losing any associations:</p>
<p># copy the users, adding 5 to the user id while we&#8217;re at it:<br />
INSERT INTO db_target.users ( id, name ) SELECT (id+5) AS id, name FROM db_source.users;</p>
<p># copy the posts, adding 5 to the user_id and 100 to the post id:<br />
INSERT INTO db_target.posts ( id, user_id, title, post ) SELECT (id+100) AS id, (user_id+5) AS user_id, title, post FROM db_source.posts;</p>
<p>Note that I have selected &#8220;as&#8221; where I have been using the addition operator. Given that we are also specifying which columns we are updating I&#8217;m not 100% sure that it&#8217;s necessary, but I find it helpful anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://otaqui.com/blog/396/performing-operations-on-mysql-data-while-copying-between-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
