2010-10-02 7 views
1

Ok, c'est ici. J'ai deux tables: produits et product_sizesjointure interne tables mysql

donc en gros ma table de produit a id (clé primaire), nom (nom du produit) et SIZE_ID (clé étrangère de product_sizes)

ma table product_sizes a des valeurs prédéterminées:

size_id  name 
------------------ 
1   1x1 
2   2x2 
3   3x3 

et ici j'ai un code de travail affichant les tableaux de produits (en html en utilisant tout code):

id  name   size_id 
----------------------------- 
1  product 1   1 
2  product 2   2 
3  product 3   1 

mon problème est que je veux affichage (en html) le nom de la taille plutôt que son SIZE_ID, quelque chose de semblable à ces exemples:

id  name   size_id 
----------------------------- 
1  product 1  1x1 
2  product 2  2x2 
3  product 3  1x1 

quelqu'un peut me apprendre comment faire .. je besoin de toute urgence pour mon projet final. Je vous remercie. désolé pour mon explication je ne suis pas bon en anglais.

Répondre

2

Utilisez un JOIN:

SELECT p.id, 
     p.name, 
     ps.name AS size_name 
    FROM PRODUCT p 
    JOIN PRODUCT_SIZES ps ON ps.size_id = p.size_id 

Voir this link of a visual representation of the various JOINs.

<? include ("conn.php"); 
    $sql = "SELECT p.id, 
        p.name, 
        ps.name AS size_name 
      FROM PRODUCT p 
      JOIN PRODUCT_SIZES ps ON ps.size_id = p.size_id"; 
    $result = mysql_query($sql, $connection) or die(mysql_error()); 
    while($row = mysql_fetch_array($result)) { 
?> 
<tr> 
    <td><? echo $row['id']; ?></td> 
    <td><? echo $row['name']; ?></td> 
    <td><? echo $row['size_name']; ?></td> 
</tr> 
<? } ?> 
+0

comment l'afficherais-je en html? –

+0

@kester martinez: J'ai ajouté la partie PHP ... –

+0

merci mal essayer maintenant –