Php-mysql Course

The JOIN command combines records from two tables by using values common to each. Usually, it is used an ID column in both tables that associates their values.
There are various types of JOINs: INNER JOIN, LEFT JOIN, RIGHT JOIN; they are used with an ON clause that sets the join condition.

- In the examples presented in this tutorial we'll use the following two tables, named "categories" and "links".
categories
idcategory
1 PHP-MySQL
2 HTML
3 JavaScript
links
idlink
1 https://coursesweb.net/php-mysql/arrays
1 https://coursesweb.net/php-mysql/strings
2 https://coursesweb.net/html/html-tables

INNER JOIN

INNER JOIN returns rows when there is at least one match in both tables (can be regarded as the default join-type).
Syntax:
SELECT column(s) FROM `table1` INNER JOIN `table2` ON `table1`.`column`=`table2`.`column`
If there are rows in "table1.column" that do not have matches in "table2.column", those rows will NOT be listed.

- Example: Select the "category" column from 'categories', and the "link" column from the 'links' table, where the value of the "id" columns is the same in both tables, ordered by "category".
SELECT `categories`.`category`, `links`.`link` FROM `categories` INNER JOIN `links` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`
Result:
|  category |                link                  |
----------------------------------------------------
| HTML      | https://coursesweb.net/html/html-tables  |
| PHP-MySQL | https://coursesweb.net/php-mysql/arrays  |
| PHP-MySQL | https://coursesweb.net/php-mysql/strings |

• INNER JOIN is the same as JOIN, and can also be replaced with a WHERE clause. So, the following queries returns the same results as the example above.
- Without the INNER keyword:
SELECT `categories`.`category`, `links`.`link` FROM `categories` JOIN `links` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`
- Or, with WHERE:
SELECT `categories`.`category`, `links`.`link` FROM `categories`, `links` WHERE `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`

LEFT JOIN

The LEFT JOIN returns all rows from the "left" table (table1), even if the join-condition does not find any matching record in the "right" table (table2).
Syntax:
SELECT column(s) FROM `table1` LEFT JOIN `table2` ON `table1`.`column`=`table2`.`column`
If there is no matching row for the "table2" in the ON part, the join will still return a row in the result, but with NULL in each column from "table2".

Example: We select again the "category" column from 'categories', and the "link" column from the 'links' table, where the value of the "id" columns is the same in both tables, but this time using LEFT JOIN.
SELECT `categories`.`category`, `links`.`link` FROM `categories` LEFT JOIN `links` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`
Result (compare with the result of the example above to see the diference):
|  category  |               link                   |
-----------------------------------------------------
| HTML       | https://coursesweb.net/html/html-tables  |
| JavaScript | NULL                                 |
| PHP-MySQL  | https://coursesweb.net/php-mysql/arrays  |
| PHP-MySQL  | https://coursesweb.net/php-mysql/strings |

• Because LEFT JOIN returns all the rows from the left table (categories), even if there are no matches in the right table (links), we can use this fact to find rows in a table that have no counterpart in another table.
In the next example we'll find all rows in "categories" with an "id" value that is not present in "links" table:
SELECT `categories`.* FROM `categories` LEFT JOIN `links` ON `categories`.`id`=`links`.`id` WHERE `links`.`id` IS NULL
Result:
| id |  category  |
-------------------
| 3  | JavaScript |

RIGHT JOIN

The RIGHT JOIN returns all the values from the "right" table (table2) and matched values from the "left" table (table1) (NULL in case of no matching join predicate).
Syntax:
SELECT column(s) FROM `table1` RIGHT JOIN `table2` ON `table1`.`column`=`table2`.`column`

Example: We select again the "category" column from 'categories', and the "link" column from the 'links' table, where the value of the "id" columns is the same in both tables, but this time using RIGHT JOIN.
SELECT `categories`.`category`, `links`.`link` FROM `categories` RIGHT JOIN `links` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`
Result (compare with the result of the example above to see the diference):
|  category  |               link                   |
-----------------------------------------------------
| HTML       | https://coursesweb.net/html/html-tables  |
| PHP-MySQL  | https://coursesweb.net/php-mysql/arrays  |
| PHP-MySQL  | https://coursesweb.net/php-mysql/strings |

• In practice, RIGHT JOIN is rarely used, since it can always be replaced with LEFT JOIN (with the table order switched). The result above is produced also with a LEFT JOIN:
SELECT `categories`.`category`, `links`.`link` FROM `links` LEFT JOIN `categories` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
MySQL INNER JOIN, LEFT JOIN, RIGHT JOIN

Last accessed pages

  1. Get Mime Type of file or string content in PHP (6230)
  2. Countdown Timer with starting time added into a form (11533)
  3. Disable button and Enable it after specified time (17533)
  4. JpGraph - Create Graph, Charts, Plots in PHP (3933)
  5. Simple Admin Login PHP Script (10999)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)