Manchmal will man 2, 3 oder mehr Tabellen miteinander verbinden, dazu gibt es join.
Während 2 bis 3 Tabellen verbinden recht einfach ist, kann es bei 4 oder mehr Tabellen schon etwas schwieriger werden.
Bei 4 oder mehr Tabellen sollten die Tabellen richtig angepasst werden.
Hier mal join mit 2 und 3 Tabellen:
2 tabellen
- CREATE TABLE a_table
- (
- id int ( 10 ) unsigned NOT NULL auto_increment ,
- product varchar ( 50 ) NOT NULL ,
- sorte varchar ( 50 ) NOT NULL ,
- a_id int ( 10 ) NOT NULL ,
- PRIMARY KEY ( id )
- ) ;
- INSERT INTO a_table VALUES ( 1 , 'Kannen' , 'Keramik' , '3' ) ;
- INSERT INTO a_table VALUES ( 2 , 'Schuhe' , 'Damen' , '1' ) ;
- INSERT INTO a_table VALUES ( 3 , 'Schuhe' , 'Herren' , '1' ) ;
- INSERT INTO a_table VALUES ( 4 , 'Karten' , 'Skat' , '2' ) ;
- INSERT INTO a_table VALUES ( 5 , 'Tapes' , 'Mobil' , '5' ) ;
- INSERT INTO a_table VALUES ( 6 , 'Handy' , 'Apple' , '4' ) ;
- CREATE TABLE b_table
- (
- a_id int ( 10 ) unsigned NOT NULL auto_increment ,
- bezeichnung varchar ( 50 ) NOT NULL ,
- standort varchar ( 50 ) NOT NULL ,
- PRIMARY KEY ( a_id )
- ) ;
- INSERT INTO b_table VALUES ( 1 , 'Sport' , 'Berlin' ) ;
- INSERT INTO b_table VALUES ( 2 , 'Spiele' , 'Bonn' ) ;
- INSERT INTO b_table VALUES ( 3 , 'Haushalt' , 'Bremen' ) ;
- INSERT INTO b_table VALUES ( 4 , 'Mobil' , 'Kiel' ) ;
- INSERT INTO b_table VALUES ( 5 , 'Technik' , 'Ulm' ) ;
- INSERT INTO b_table VALUES ( 6 , 'Umwelt' , 'Köln' ) ;
die foo.php
<?php
- echo '<table> ' ;
- echo ' <tr> ' ;
- echo ' <td colspan="2"> Tabelle: a_table </td> ' ;
- echo ' <td colspan="2"> Tabelle: b_table </td> ' ;
- echo ' </tr> ' ;
- echo ' <tr> ' ;
- echo ' <th> Prod.: </th> ' ;
- echo ' <th> Sorte: </th> ' ;
- echo ' <th> Abteil.: </th> ' ;
- echo ' <th> Ort: </th> ' ;
- echo ' </tr> ' ;
- // Datenbank verbindung einfügen
- $stmt = $sqli -> prepare ( "
- SELECT
- a_table.product , a_table.sorte , a_table.a_id , b_table.bezeichnung , b_table.standort , b_table.a_id
- FROM
- a_table
- JOIN
- b_table
- ON
- a_table.a_id = b_table.a_id
- " ) ;
- $stmt -> execute ( ) ;
- $result = $stmt -> get_result ( ) ;
- while ( $line = $result -> fetch_assoc ( ) )
- $stmt = $pdo -> prepare ( "
- SELECT
- a_table.product , a_table.sorte , a_table.a_id , b_table.bezeichnung , b_table.standort , b_table.a_id
- FROM
- a_table
- JOIN
- b_table
- ON
- a_table.a_id = b_table.a_id
- " ) ;
- $stmt -> execute ( ) ;
- while ( $line = $stmt -> fetch ( ) )
- {
- echo ' <tr> ' ;
- echo ' <td> '.$line [ ' product ' ].' </td> ' ;
- echo ' <td> '.$line [ ' sorte ' ].' </td> ' ;
- echo ' <td> '.$line [ ' bezeichnung ' ].' </td> ' ;
- echo ' <td> '.$line [ ' standort ' ].' </td> ' ;
- echo ' </tr> ' ;
- }
- echo'</table>'; ?>
Ausgabe formatiert:
Tabelle:
a_table
Tabelle:
b_table
Prod.:
Sorte:
Abteil.:
Ort:
Kannen
Keramik
Haushalt
Bremen
Schuhe
Damen
Sport
Berlin
Schuhe
Herren
Sport
Berlin
Karten
Skat
Spiele
Bonn
Tapes
Mobil
Technik
Ulm
Handy
Apple
Mobil
Kiel
3 tabellen
- CREATE TABLE a_table
- (
- id int ( 10 ) unsigned NOT NULL auto_increment ,
- product varchar ( 50 ) NOT NULL ,
- sorte varchar ( 50 ) NOT NULL ,
- a_id int ( 10 ) NOT NULL ,
- PRIMARY KEY ( id )
- ) ;
- INSERT INTO a_table VALUES ( 1 , 'Kannen' , 'Keramik' , '3' ) ;
- INSERT INTO a_table VALUES ( 2 , 'Schuhe' , 'Damen' , '1' ) ;
- INSERT INTO a_table VALUES ( 3 , 'Schuhe' , 'Herren' , '1' ) ;
- INSERT INTO a_table VALUES ( 4 , 'Karten' , 'Skat' , '2' ) ;
- INSERT INTO a_table VALUES ( 5 , 'Tapes' , 'Mobil' , '5' ) ;
- INSERT INTO a_table VALUES ( 6 , 'Handy' , 'Apple' , '4' ) ;
- CREATE TABLE b_table
- (
- a_id int ( 10 ) unsigned NOT NULL auto_increment ,
- bezeichnung varchar ( 50 ) NOT NULL ,
- standort varchar ( 50 ) NOT NULL ,
- PRIMARY KEY ( a_id )
- ) ;
- INSERT INTO b_table VALUES ( 1 , 'Sport' , 'Berlin' ) ;
- INSERT INTO b_table VALUES ( 2 , 'Spiele' , 'Bonn' ) ;
- INSERT INTO b_table VALUES ( 3 , 'Haushalt' , 'Bremen' ) ;
- INSERT INTO b_table VALUES ( 4 , 'Mobil' , 'Kiel' ) ;
- INSERT INTO b_table VALUES ( 5 , 'Technik' , 'Ulm' ) ;
- INSERT INTO b_table VALUES ( 6 , 'Umwelt' , 'Köln' ) ;
- CREATE TABLE c_table
- (
- id int ( 10 ) unsigned NOT NULL auto_increment ,
- person varchar ( 50 ) NOT NULL ,
- nachname varchar ( 50 ) NOT NULL ,
- PRIMARY KEY ( id )
- ) ;
- INSERT INTO c_table VALUES ( 1 , 'Frau' , 'Niem' ) ;
- INSERT INTO c_table VALUES ( 2 , 'Herr' , 'Keim' ) ;
- INSERT INTO c_table VALUES ( 3 , 'Frau' , 'Gross' ) ;
- INSERT INTO c_table VALUES ( 4 , 'Herr' , 'Holm' ) ;
- INSERT INTO c_table VALUES ( 5 , 'Herr' , 'Nales' ) ;
- INSERT INTO c_table VALUES ( 6 , 'Frau' , 'Brand' ) ;
die foo.php
<?php
- echo '<table> ' ;
- echo ' <tr> ' ;
- echo ' <td colspan="2"> Tabelle: a_table </td> ' ;
- echo ' <td colspan="2"> Tabelle: b_table </td> ' ;
- echo ' <td colspan="2"> Tabelle: c_table </td> ' ;
- echo ' </tr> ' ;
- echo ' <tr> ' ;
- echo ' <th> Prod.: </th> ' ;
- echo ' <th> Sorte: </th> ' ;
- echo ' <th> Abteil.: </th> ' ;
- echo ' <th> Ort: </th> ' ;
- echo ' <th> Person: </th> ' ;
- echo ' <th> Name: </th> ' ;
- echo ' </tr> ' ;
- // Datenbank verbindung einfügen
- $stmt = $sqli -> prepare ( "
- SELECT
- a_table.product , a_table.sorte , a_table.a_id , b_table.bezeichnung , b_table.standort , b_table.a_id , c_table.nachname , c_table.person , c_table.id
- FROM
- a_table
- JOIN
- b_table
- ON
- a_table.a_id = b_table.a_id
- JOIN
- c_table
- ON
- a_table.a_id = c_table.id
- " ) ;
- $stmt -> execute ( ) ;
- $result = $stmt -> get_result ( ) ;
- while ( $line = $result -> fetch_assoc ( ) )
- $stmt = $pdo -> prepare ( "
- SELECT
- a_table.product , a_table.sorte , a_table.a_id , b_table.bezeichnung , b_table.standort , b_table.a_id , c_table.nachname , c_table.person , c_table.id
- FROM
- a_table
- JOIN
- b_table
- ON
- a_table.a_id = b_table.a_id
- JOIN
- c_table
- ON
- a_table.a_id = c_table.id
- " ) ;
- $stmt -> execute ( ) ;
- while ( $line = $stmt -> fetch ( ) )
- {
- echo ' <tr> ' ;
- echo ' <td> '.$line [ ' product ' ].' </td> ' ;
- echo ' <td> '.$line [ ' sorte ' ].' </td> ' ;
- echo ' <td> '.$line [ ' bezeichnung ' ].' </td> ' ;
- echo ' <td> '.$line [ ' standort ' ].' </td> ' ;
- echo ' <td> '.$line [ ' person ' ].' </td> ' ;
- echo ' <td> '.$line [ ' nachname ' ].' </td> ' ;
- echo ' </tr> ' ;
- }
- echo'</table>'; ?>
Ausgabe formatiert:
Tabelle:
a_table
Tabelle:
b_table
Tabelle:
c_table
Prod.:
Sorte:
Abteil.:
Ort:
Person:
Name:
Kannen
Keramik
Haushalt
Bremen
Frau
Gross
Schuhe
Damen
Sport
Berlin
Frau
Niem
Schuhe
Herren
Sport
Berlin
Frau
Niem
Karten
Skat
Spiele
Bonn
Herr
Kaim
Tapes
Mobil
Technik
Ulm
Herr
Nales
Handy
Apple
Mobil
Kiel
Herr
Holm