Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like this:</p> <pre><code>SELECT posts.*,, users.Username FROM posts INNER JOIN users ON posts.ref_id = users.user_id; </code></pre> <hr> <h2>Explanation:</h2> <p>To <code>JOIN</code> to any tables with each others, there are two things; the <code>JOIN</code> type and the join condition. There are three main types of join:</p> <ul> <li><p><code>INNER JOIN</code>, only the rows that match the join condition will be returned from the two tables no more rows. But:</p></li> <li><p><code>LEFT OUTER JOIN</code>, when you join two tables you will have one on the left of the join keyword and the other one will be in the right:</p> <p>FROM Table1 &lt;------------- This is the left table. LEFT OUTER JOIN table2 .... &lt;------------- This is the right table.</p></li> </ul> <p>In <code>LEFT OUTER JOIN</code> the unmatched rows from the left table will be included in the result set.</p> <ul> <li><p><code>RIGHT OUTER JOIN</code> the unmatched rows from the right table will be included in the result set.</p></li> <li><p><code>CROSS JOIN</code> this will perform a Cartesian product from the two tables.</p></li> </ul> <p>In our query, the query will reutrn all the users from the <code>users</code> table only if the <code>ref_id</code> equal to the <code>user_id</code> column form the <code>posts</code> table.</p> <p>For more information and explanations:</p> <ul> <li><p><a href="http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html" rel="nofollow"><strong>A Visual Explanation of SQL Joins</strong></a>.</p></li> <li><p><a href="http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins" rel="nofollow"><strong>Another Visual Representation of SQL Joins</strong></a>.</p></li> <li><p><a href="http://dev.mysql.com/doc/refman/5.0/en/join.html" rel="nofollow"><strong>Join syntax in MySQL</strong></a></p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload