Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first two queries are equivalent; in the MySql world the using keyword is (well, almost - see the <a href="http://dev.mysql.com/doc/refman/5.0/en/join.html" rel="noreferrer">documentation</a> but using is part of the Sql2003 spec and there are some differences in NULL values) the same as saying field1.id = field2.id</p> <p>You could easily write them as:</p> <pre><code>SELECT field1, field2 FROM table1 INNER JOIN table2 ON (table1.id = table2.id) </code></pre> <p>The third query is a LEFT JOIN. This will select all the matching rows in both tables, and will also return all the rows in table1 that have no matches in table2. For these rows, the columns in table2 will be represented by NULL values.</p> <p>I like Jeff Atwood's <a href="http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html" rel="noreferrer">visual explanation</a> of these</p> <p>Now, on to what is better or worse. The answer is, it <strong>depends</strong>. They are for different things. If there are more rows in table1 than table2, then a left join will return more rows than an inner join. But the performance of the queries will be effected by many factors, like table size, the types of the column, what the database is doing at the same time.</p> <p>Your first concern should be to use the query you need to get the data out. You might honestly want to know what rows in table1 have no match in table2; in this case you'd use a LEFT JOIN. Or you might only want rows that match - the INNER JOIN.</p> <p>As Krister points out, you can use the <a href="http://docs.oracle.com/cd/E17952_01/refman-5.5-en/explain.html" rel="noreferrer">EXPLAIN</a> keyword to tell you how the database will execute each kind of query. This is very useful when trying to figure out just why a query is slow, as you can see where the database spends all of its time.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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