online: 5; azi: 788; total: 52794 Webdesign - Phpmysql - 35

MySQL INNER JOIN, LEFT JOIN, RIGHT JOIN

Comanda JOIN combina inregistrarile din doua tabele, folosind valorile comune din fiecare.
Sunt mai multe tipuri de JOIN: INNER JOIN, LEFT JOIN, RIGHT JOIN; se folosesc cu o clauza ON ce seteaza conditia de unire.

- In exemplele prezentate in acest tutorial se folosesc urmatoarele doua tabele, denumite "categories" si "links".
categories
idcategory
1 PHP-MySQL
2 HTML
3 JavaScript
links
idlink
1 www.discant.ro/php-mysql/matrice_tablouri.html
1 www.discant.ro/php-mysql/siruri.html
2 www.discant.ro/html/tabele.html

INNER JOIN

INNER JOIN returneaza randurile in care exista o potrivire a conditiei in ambele tabele (poate fi considerat ca tipul standard de unire).
Sintaxa:
SELECT coloana(e) FROM `tabel1` INNER JOIN `tabel2` ON `tabel1`.`coloana`=`tabel2`.`coloana`
Daca sunt randuri in "tabel1.coloana" care nu au o potrivire de egalitate in "tabel2.coloana", acele randuri nu vor fi selectate.

- Exemplu: Selecteaza inregistrarile din coloana "category" (din 'categories'), si din coloana "link" (din tabelul 'links'), unde valoarea coloanei "id" este aceeasi in ambele tabele; ordonate dupa "category".
SELECT `categories`.`category`, `links`.`link` FROM `categories` INNER JOIN `links` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`
Rezultat:
|  category |                     link                       |
--------------------------------------------------------------
| HTML      | www.discant.ro/html/tabele.html                |
| PHP-MySQL | www.discant.ro/php-mysql/matrice_tablouri.html |
| PHP-MySQL | www.discant.ro/php-mysql/siruri.html           |

• INNER JOIN e la fel ca JOIN, si de asemenea poate fi inlocuit cu o clauza WHERE. Astfel, urmatoarele doua interogari SQL returneaza acelasi rezultat ca exemplul de sus.
- Fara cuvantul INNER:
SELECT `categories`.`category`, `links`.`link` FROM `categories` JOIN `links` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`
- Sau, cu WHERE:
SELECT `categories`.`category`, `links`.`link` FROM `categories`, `links` WHERE `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`

LEFT JOIN

Comanda LEFT JOIN returneaza toate randurile din tabelul din "stanga" (tabel1), indiferent daca conditia de unire nu se potriveste cu inregistrarile din al doilea tabel, din "dreapta (tabel2).
Sintaxa:
SELECT coloana(e) FROM `tabel1` LEFT JOIN `tabel2` ON `tabel1`.`coloana`=`tabel2`.`coloana`
Daca exista randuri din "tabel2" care nu se potrivesc cu conditia de la partea ON, "join" va adauga acele randuri in rezultat, dar cu valoarea NULL la "tabel2".

Exemplu: Vom selecta din nou inregistrarile din coloana "category" (din 'categories'), si din coloana "link" (din tabelul 'links'), unde valoarea coloanei "id" este aceeasi in ambele tabele, dar de data aceasta cu LEFT JOIN.
SELECT `categories`.`category`, `links`.`link` FROM `categories` LEFT JOIN `links` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`
Rezultat (comparati cu rezultatul din exemplu de mai sus ca sa vedeti diferenta):
|  category  |                    link                        |
---------------------------------------------------------------
| HTML       | www.discant.ro/html/tabele.html                |
| JavaScript | NULL                                           |
| PHP-MySQL  | www.discant.ro/php-mysql/matrice_tablouri.html |
| PHP-MySQL  | www.discant.ro/php-mysql/siruri.html           |

• Deoarece LEFT JOIN returneaza toate randurile din primul tabel (categories), indiferent daca exista potrivire cu randuri din al doilea tabel (links), se poate folosi acest fapt pentru a gasi randurile dintr-un tabel care nu au un corespondet in alt tabel.
In urmatorul exemplu vom gasi randurile din "categories" cu o valoare a coloanei "id" care nu e prezenta in tabelul "links":
SELECT `categories`.* FROM `categories` LEFT JOIN `links` ON `categories`.`id`=`links`.`id` WHERE `links`.`id` IS NULL
Rezultat:
| id |  category  |
-------------------
| 3  | JavaScript |

RIGHT JOIN

RIGHT JOIN returneaza toate valorile din al doilea tabel, din "dreapta" (tabel2) si inregistrarile potrivite din primul tabel (tabel1) (NULL in caz de rand corespondent nepotrivit).
Sintaxa:
SELECT coloana(e) FROM `tabel1` RIGHT JOIN `tabel2` ON `tabel1`.`coloana`=`tabel2`.`coloana`

Exemplu: Vom selecta iar inregistrarile din coloana "category" (din 'categories'), si din coloana "link" (din tabelul 'links'), unde valoarea coloanei "id" este aceeasi in ambele tabele, dar de data aceasta cu RIGHT JOIN.
SELECT `categories`.`category`, `links`.`link` FROM `categories` RIGHT JOIN `links` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`
Rezultat (comparati cu rezultatul din exemplu de mai sus ca sa vedeti diferenta):
|  category  |                    link                        |
---------------------------------------------------------------
| HTML       | www.discant.ro/html/tabele.html                |
| PHP-MySQL  | www.discant.ro/php-mysql/matrice_tablouri.html |
| PHP-MySQL  | www.discant.ro/php-mysql/siruri.html           |

• In practica, RIGHT JOIN se foloseste rar, deoarece poate fi inlocuit cu LEFT JOIN (prin inversarea ordinii tabelelor). Rezultatul de mai sus poate fi obtinut si cu urmatoarea interogare SELECT, cu LEFT JOIN:
SELECT `categories`.`category`, `links`.`link` FROM `links` LEFT JOIN `categories` ON `categories`.`id`=`links`.`id` ORDER BY `categories`.`category`