<?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>Aaron Jefferson Villanueva&#039;s Blog Site&#187; MySql Join query</title>
	<atom:link href="http://malebolgia.shadowsonawall.net/tag/mysql-join-query/feed" rel="self" type="application/rss+xml" />
	<link>http://malebolgia.shadowsonawall.net</link>
	<description>Programming / Designing Blogging from windows to mac.</description>
	<lastBuildDate>Fri, 20 May 2011 12:05:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Right way to use sql&#8230;</title>
		<link>http://malebolgia.shadowsonawall.net/code-programming/right-way-to-use-sql-join-with-conditions-displaying-no-match-data.html</link>
		<comments>http://malebolgia.shadowsonawall.net/code-programming/right-way-to-use-sql-join-with-conditions-displaying-no-match-data.html#comments</comments>
		<pubDate>Tue, 03 Mar 2009 07:27:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding/Programming]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySql Join query]]></category>

		<guid isPermaLink="false">http://malebolgia.shadowsonawall.net/?p=55</guid>
		<description><![CDATA[Right way to use sql join with conditions (Displaying no match data). 

After hours trying to figure out what's wrong with my query for displaying list of cities with how many Lease/Sale were there. The problem was getting the cities even not yet added on Lease or Sale type, that means I need to list [...]]]></description>
			<content:encoded><![CDATA[<strong>Right way to use sql join with conditions (Displaying no match data). </strong><br />

After hours trying to figure out what's wrong with my query for displaying list of cities with how many Lease/Sale were there. The problem was getting the cities even not yet added on Lease or Sale type, that means I need to list cities that has no match yet or 0 count. <br /><br />


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$mainframe</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ebconfig</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$mainframe</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParams</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$prop_Type</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ebconfig</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prop_type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Lease&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$db_city</span> <span style="color: #339933;">=&amp;</span> JFactory<span style="color: #339933;">::</span><span style="color: #004000;">getDBO</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query_city</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT t1.city, t1.id, COUNT( t2.propCity ) AS city_count
FROM #__listing_city AS t1
LEFT OUTER JOIN #__listing_listings AS t2 ON t2.propCity = t1.city
WHERE t2.propType = '<span style="color: #006699; font-weight: bold;">$prop_Type</span>' AND t2.published = 1
GROUP BY t1.city
ORDER BY t1.city ASC&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>



<br /><br />
The code above does not display the cities that has no match on another table. The where clause was giving the problem...
<br />


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$mainframe</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ebconfig</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$mainframe</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParams</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$prop_Type</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ebconfig</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prop_type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Lease&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$db_city</span> <span style="color: #339933;">=&amp;</span> JFactory<span style="color: #339933;">::</span><span style="color: #004000;">getDBO</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query_city</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT t1.city, t1.id, COUNT( t2.propCity ) AS city_count
FROM #__listing_city AS t1
LEFT OUTER JOIN #__listing_listings AS t2 ON t2.propCity = t1.city
AND t2.published = 1 AND t2.propType = '<span style="color: #006699; font-weight: bold;">$prop_Type</span>'
GROUP BY t1.city
ORDER BY t1.city ASC&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>




<br /><br />

Instead putting the condition on "ON Clause" after the matching table condition, fixed the problem.]]></content:encoded>
			<wfw:commentRss>http://malebolgia.shadowsonawall.net/code-programming/right-way-to-use-sql-join-with-conditions-displaying-no-match-data.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

