<?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>Kevin Bradwick &#187; join</title>
	<atom:link href="http://www.kevinbradwick.co.uk/tag/join/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kevinbradwick.co.uk</link>
	<description>Web development and design blog</description>
	<lastBuildDate>Fri, 09 Jul 2010 08:11:48 +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>SQL JOIN statements</title>
		<link>http://www.kevinbradwick.co.uk/2010/02/sql-join-statements/</link>
		<comments>http://www.kevinbradwick.co.uk/2010/02/sql-join-statements/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 16:35:01 +0000</pubDate>
		<dc:creator>kevin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[join]]></category>

		<guid isPermaLink="false">http://www.kevinbradwick.co.uk/?p=122</guid>
		<description><![CDATA[When working with relational databases, you will often find that the data your require can reside on multiple tables. In simple terms, a join statement brings together these tables using a common identifier. Joins are the best way to query a database whilst improving transaction efficiency. An example We&#8217;ll create two tables, a users and [...]]]></description>
			<content:encoded><![CDATA[<p>When working with relational databases, you will often find that the data your require can reside on multiple tables. In simple terms, a join statement brings together these tables using a common identifier. Joins are the best way to query a database whilst improving transaction efficiency.</p>
<h3>An example</h3>
<p>We&#8217;ll create two tables, a users and account table. The account table holds account names (e.g. Guest, Admin, Super Admin) and the users table contains one foreign key that relates to the account type.</p>
<p>The structure looks like this;</p>
<p><img class="alignnone size-thumbnail wp-image-123" title="schema" src="http://www.kevinbradwick.co.uk/wp-content/uploads/2010/02/schema.png" alt="" /></p>
<p>And here&#8217;s some data that we&#8217;ll work with&#8230;</p>
<p><img class="alignnone size-thumbnail wp-image-126" title="sql-select-accounts" src="http://www.kevinbradwick.co.uk/wp-content/uploads/2010/02/sql-select-accounts.png" alt="" /></p>
<p><img class="alignnone size-full wp-image-130" title="users-data" src="http://www.kevinbradwick.co.uk/wp-content/uploads/2010/02/users-data.png" alt="" width="392" height="161" /></p>
<p>Now to perform a simple join of these two tables, we could use&#8230;</p>
<p><img class="alignnone size-full wp-image-132" title="join-1" src="http://www.kevinbradwick.co.uk/wp-content/uploads/2010/02/join-1.png" alt="" width="544" height="139" /></p>
<p>This simple query will return all records but using this as a base we can further customise our query to make it more useful. If we wanted to find out all users who have Admin accounts we&#8217;d use this statement.</p>
<pre name="code" class="php">
SELECT * FROM users u LEFT JOIN accounts a ON u.account_type = a.id WHERE a.name = 'Admin'

// or we could use the ID from the accounts table

SELECT * FROM users u LEFT JOIN accounts a ON u.account_type = a.id WHERE a.id = 2
</pre>
<h3>A note on usage</h3>
<p>This is MySQL join in its most simplest terms. You can join multiple tables providing you database design allows it. So with that cleared up, if you are not already using joins in your queries, start using them and cut down on the number of transactions you make&#8230;it&#8217;ll also make your life a lot easier retrieving data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinbradwick.co.uk/2010/02/sql-join-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
