<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8411073052722123531</id><updated>2024-08-31T05:56:07.375-07:00</updated><title type='text'>MySQL Tunning &amp;amp; Performance Tweaks by Ruskevych Valentin</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mysqltunning.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8411073052722123531/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://mysqltunning.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00693608024488309848</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8411073052722123531.post-2337064295876277762</id><published>2012-06-22T15:00:00.000-07:00</published><updated>2012-06-22T15:00:33.550-07:00</updated><title type='text'>MySQL Joins Tutorial (Left, Right, Inner, Nature, self)</title><content type='html'>Today we are going to discuss &lt;strong&gt;MySQL Joins (Left Join, Right Join, Inner Join, Self Join and Natural Join)&lt;/strong&gt; in additional to these, we will touch the &lt;strong&gt;Straight Join (STRAIGHT_JOIN)&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;

Let&#39;s we begin from joins syntax.
The syntax is as follows:
&lt;pre class=&quot;brush: sql&quot;&gt;
SELECT 
   t1.col1, 
   t2.col2
FROM
   table1 AS t1
   JOIN 
      table2 AS t2
         ON (t1.col1 = t2.col2)
WHERE t1.col1=t2.col2
&lt;/pre&gt;
SELECT as usual, then going FROM table JOIN, join another table to the first table and then ON. join ON (condition) after maybe used WHERE clause, but not necessary.&lt;br/&gt;
In total ON is equal to WHERE and is not limited to one condition, then you can append GROUP, HAVING, LIMIT, etc.&lt;br/&gt;&lt;br/&gt;
Now let&#39;s discuss the joins type, they are few and we will talk about each of them.&lt;br/&gt;

&lt;strong&gt;MySQL&#39;s INNER JOIN / JOIN &lt;/strong&gt;&lt;br/&gt;
INNER JOIN is equal to JOIN. next 2 statements are equal by their meaning:&lt;br/&gt;
&lt;pre class=&quot;brush: sql&quot;&gt;
SELECT 
   t1.col1, t1.col2
FROM
   table1
   INNER JOIN table2
   ON (t1.col1 = t2.col2)

# previous query is equal to next
SELECT 
   t1.col1, t1.col2
FROM
   table1
   JOIN table2
   ON (t1.col1 = t2.col2)
&lt;/pre&gt;&lt;br/&gt;
INNER JOIN return rows when there is at least one match in both tables, otherwise the query wouldn&#39;t return results.
&lt;br/&gt;&lt;br/&gt;
&lt;strong&gt;MySQL&#39;s LEFT JOIN&lt;/strong&gt;&lt;br/&gt;
LEFT JOIN return ALL rows from the left table even if no match found on the right table. example:&lt;br/&gt;
&lt;pre class=&quot;brush: sql&quot;&gt;
SELECT 
   t1.col1, t1.col2
FROM
   table1 # returns all rows from table 1
   LEFT JOIN table2 # even f no match in table 2
   ON (t1.col1 = t2.col2)
&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;
&lt;strong&gt;MySQL&#39;s RIGHT JOIN&lt;/strong&gt;&lt;br/&gt;
RIGHT JOIN is a reversed join of left, returns all rows from right table even if no match found in left. refer to previous example of left join.&lt;br/&gt;&lt;br/&gt;
&lt;strong&gt;NATURAL JOIN&lt;/strong&gt;&lt;br/&gt;
NATURAL JOIN acts as INNER JOIN, the difference is that INNER JOIN may return any of the columns while NATURAL JOIN will return only these with same name in both tables.&lt;br/&gt;
NATURAL JOIN also doesn&#39;t apply ON clause.
&lt;br/&gt;&lt;br/&gt;
&lt;strong&gt;SELF JOIN&lt;/strong&gt;&lt;br/&gt;
SELF JOIN is a kind of JOIN, SELF JOIN can be any of these, INNER, LEFT or RIGHT.&lt;br/&gt;
SELF JOIN joins same table how many times you required.
example:
&lt;pre class=&quot;brush: sql&quot;&gt;
SELECT
   *
FROM
   table1 AS t1
   JOIN table1 AS t2
      ON (t1.col1&lt;&gt;t2.col1)
&lt;/pre&gt;&lt;br/&gt;
In this example we joined same table, this is kind of implementation of SELF JOIN.
&lt;br/&gt;&lt;br/&gt;
&lt;strong&gt;STAIGHT_JOIN&lt;/strong&gt;&lt;br/&gt;
STRAIGHT_JOIN is a keyword that tells (forces) MySQL&#39;s Optimizer to join tables in order they are type in the query, this have it&#39;s benefits and disadvantages.&lt;br/&gt;
This kind of join may be seen very useful however no one will advice to use them always.&lt;br/&gt;
Use EXPLAIN/EXPLAIN EXTENDED to understand whether to use it or not.&lt;br/&gt;
&lt;pre class=&quot;brush: sql&quot;&gt;
SELECT
   *
FROM
   table1 AS t1
   STRAIGHT_JOIN table1 AS t2
      ON (t1.col1&lt;&gt;t2.col1)
&lt;/pre&gt;
&lt;br/&gt;In this example, no matter what will happen, optimizer will read t1 and then will read table2 in an inner loop.&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;
Have fun playing these things ;-)&lt;br/&gt;
Sincerely,&lt;br/&gt;
VR.</content><link rel='replies' type='application/atom+xml' href='http://mysqltunning.blogspot.com/feeds/2337064295876277762/comments/default' title='Комментарии к сообщению'/><link rel='replies' type='text/html' href='http://mysqltunning.blogspot.com/2012/06/mysql-joins-tutorial-left-right-inner.html#comment-form' title='Комментарии: 2'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8411073052722123531/posts/default/2337064295876277762'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8411073052722123531/posts/default/2337064295876277762'/><link rel='alternate' type='text/html' href='http://mysqltunning.blogspot.com/2012/06/mysql-joins-tutorial-left-right-inner.html' title='MySQL Joins Tutorial (Left, Right, Inner, Nature, self)'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00693608024488309848</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry></feed>