<?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>thinking in geek &#187; sql</title>
	<atom:link href="http://joshrobb.com/blog/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://joshrobb.com/blog</link>
	<description>tagline's are so web2.0</description>
	<lastBuildDate>Fri, 11 Mar 2011 07:17:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Cross Join</title>
		<link>http://joshrobb.com/blog/2007/01/31/cross-join/</link>
		<comments>http://joshrobb.com/blog/2007/01/31/cross-join/#comments</comments>
		<pubDate>Wed, 31 Jan 2007 16:55:55 +0000</pubDate>
		<dc:creator>josh</dc:creator>
				<category><![CDATA[databases]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://joshrobb.com/blog/2007/01/31/cross-join/</guid>
		<description><![CDATA[I remember learning about cross joins (aka Cartesian products) in 1990/1 during my first database course. We were using some research project from Massey University which had produced a RDBMS which ran in DOS and supported SQL1. My question at the time was &#8211; what would be the use of a cross join in a [...]]]></description>
			<content:encoded><![CDATA[<p>I remember learning about cross joins (aka Cartesian products) in 1990/1 during my first database course. We were using some research project from Massey University which had produced a RDBMS which ran in DOS and supported SQL<sup><a href="http://joshrobb.com/blog/2007/01/31/cross-join/#footnote_0_7" id="identifier_0_7" class="footnote-link footnote-identifier-link" title="This was impressive on a number of levels &amp;#8211; state of the art for DOS was dbaseIII which didn&amp;#8217;t support SQL but had some twisted language all it&amp;#8217;s own.">1</a></sup>.</p>
<p>My question at the time was &#8211; what would be the use of a cross join in a practical system? In my time I&#8217;ve occasionally run a cross join by accident (forgetting a where clause or screwing up the join condition) &#8211; sometimes even on production systems with very large databases &#8211; whoops!</p>
<p>Today &#8211; I finally got to use one to solve a business problem! Here&#8217;s the scenario (bad are words censored):</p>
<pre>bad_words         comments</pre>
<pre>+---------------+ +-------+------------+------+
| word          | | id    | comments   | live |
+---------------+ +-------+------------+------+
| W*NK*RS       | |     1 | a comm...  |    1 |
| B*ST*RDS      | |     2 | another... |    1 |
| ARS*          | | 36000 | lots...    |    1 |
| M*TH*RF*CK*RS | +-------+------------+------+
| C*NTS         |    36,000 rows
| C*CKS         |
| etc.          |
+---------------+
  648 rows</pre>
<p>Two tables &#8211; one with a list of words which should not appear in the other table. The table to be scanned actually has three columns which need to be checked.</p>
<p>What I did was cross join both tables (resulting in 23,328,000 rows) and then use the where clause to filter the out the rows which don&#8217;t contain at least one bad word.</p>
<p><code>INSERT INTO bad<br />
SELECT DISTINCT c.id<br />
FROM comments c, bad_words w<br />
WHERE<br />
pros LIKE CONCAT('%',w.word,'%')<br />
OR cons LIKE CONCAT('%',w.word,'%')<br />
OR comments LIKE CONCAT('%',w.word,'%')</code></p>
<p>Note: It actually got more complicated than this &#8211; we needed to match on word boundaries &#8211; which meant using a regex &#8211; but the principle was the same.</p>
<ol class="footnotes"><li id="footnote_0_7" class="footnote">This was impressive on a number of levels &#8211; state of the art for DOS was <a href="http://en.wikipedia.org/wiki/DBASE">dbaseIII</a> which didn&#8217;t support SQL but had some twisted language all it&#8217;s own.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://joshrobb.com/blog/2007/01/31/cross-join/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

