Latest web development tutorials

Utilizzando la connessione Mysql

Nei capitoli precedenti, abbiamo imparato che se leggere i dati in una tabella, che è relativamente semplice, ma spesso la necessità di leggere i dati da più tabelle in un'applicazione reale.

In questo capitolo vi mostreremo come utilizzare MySQL ENTRA dati della query in due o più tabelle.

È possibile utilizzare Mysql in SELECT, UPDATE e DELETE per unirsi query multi-tavolo comune.

JOIN in base alla funzione suddiviso nelle seguenti tre categorie:

  • (Collegamento interno, o il collegamento equivalente) INNER JOIN: Get record di due campi nella tabella corrisponde relazioni.
  • LEFT JOIN (collegamento a sinistra): Ottieni tutti i record dalla tabella di sinistra, tavolo giusto, anche se non ci sono record corrispondenti corrispondenti.
  • RIGHT JOIN (collegamento a destra): In contrasto con il LEFT JOIN, ottenere la tabella di destra per tutti i record, anche se la tabella di sinistra non ha record corrispondenti corrispondenti.

Struttura del database e dei dati utilizzati in questa sezione Download: w3big.sql .


Utilizzare INNER JOIN prompt dei comandi

Abbiamo due tavoli tcount_tbl e w3big_tbl nel database w3big. Due dati tabelle dati sono i seguenti:

Esempi

Provare i seguenti esempi:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use w3big;
Database changed
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| w3big_author | w3big_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
| John Poul       |              1 |
| Sanjay          |              1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
mysql> SELECT * from w3big_tbl;
+-------------+----------------+-----------------+-----------------+
| w3big_id | w3big_title | w3big_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|           1 | Learn PHP      | John Poul       | 2007-05-24      |
|           2 | Learn MySQL    | Abdul S         | 2007-05-24      |
|           3 | JAVA Tutorial  | Sanjay          | 2007-05-06      |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.00 sec)
mysql>

Poi si arriva a collegare più di due tabelle nella tabella di leggere w3big_tbl tutti i valori di campo w3big_count campo w3big_author corrispondenti alla tabella in tcount_tbl utilizzando MySQL INNER JOIN (uso può essere omesso INNER JOIN, lo stesso effetto):

mysql> SELECT a.w3big_id, a.w3big_author, b.w3big_count FROM w3big_tbl a INNER JOIN tcount_tbl b ON a.w3big_author = b.w3big_author;
+-----------+---------------+--------------+
| w3big_id | w3big_author | w3big_count |
+-----------+---------------+--------------+
|         1 | John Poul     |            1 |
|         3 | Sanjay        |            1 |
+-----------+---------------+--------------+
2 rows in set (0.00 sec)

Sopra istruzione SQL è equivalente a:

mysql> SELECT a.w3big_id, a.w3big_author, b.w3big_count FROM w3big_tbl a, tcount_tbl b WHERE a.w3big_author = b.w3big_author;
+-------------+-----------------+----------------+
| w3big_id | w3big_author | w3big_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
2 rows in set (0.01 sec)
mysql>


MySQL LEFT JOIN

MySQL ha lasciato aderire e unirsi diverso. MySQL LEFT JOIN leggerà tutti i dati della tabella di dati a sinistra, il lato destro del tavolo anche se non dati corrispondenti.

Esempi

Provare i seguenti esempi, tavolo w3big_tbl sinistra, tcount_tbl il tavolo giusto, per capire MySQL LEFT JOIN applicazioni:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use w3big;
Database changed
mysql> SELECT a.w3big_id, a.w3big_author, b.w3big_count FROM w3big_tbl a LEFT JOIN tcount_tbl b ON a.w3big_author = b.w3big_author;
+-------------+-----------------+----------------+
| w3big_id | w3big_author | w3big_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           2 | Abdul S         |           NULL |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)

Gli esempi precedenti utilizzano il LEFT JOIN, si legge nella dichiarazione tutto il campo di dati selezionato a sinistra del w3big_tbl tabella di dati, anche se non vi è alcuna corrispondente tabella tcount_tbl a destra nel valore del campo w3big_author.


MySQL RIGHT JOIN

MySQL RIGHT JOIN legge tutti i dati a destra della tabella di dati sul lato sinistro del tavolo, anche se non ci sono dati corrispondenti.

Esempi

Provare i seguenti esempi, tcount_tbl tabella a sinistra, w3big_tbl il tavolo giusto, per capire MySQL RIGHT JOIN applicazioni:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use w3big;
Database changed
mysql> SELECT b.w3big_id, b.w3big_author, a.w3big_count FROM tcount_tbl a RIGHT JOIN w3big_tbl b ON a.w3big_author = b.w3big_author;
+-------------+-----------------+----------------+
| w3big_id | w3big_author | w3big_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           2 | Abdul S         |           NULL |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)

Gli esempi sopra riportati utilizzano un RIGHT JOIN, si legge nella dichiarazione tutto il campo di dati selezionato a destra della w3big_tbl tabella di dati, anche se non c'è nessuna tabella corrispondente nel tcount_tbl sinistra nel valore del campo w3big_author.


JOIN uso negli script PHP

uso di PHP mysql_query () per eseguire istruzioni SQL, è possibile utilizzare più rispetto allo stesso istruzione SQL come mysql_query) parametri di funzione (.

Provare i seguenti esempi:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT a.w3big_id, a.w3big_author, b.w3big_count FROM w3big_tbl a INNER JOIN tcount_tbl b ON a.w3big_author = b.w3big_author';

mysql_select_db('w3big');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Author:{$row['w3big_author']}  <br> ".
         "Count: {$row['w3big_count']} <br> ".
         "Tutorial ID: {$row['w3big_id']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>