problema inner join
-
Ciao a tutti, sto costruendo un cms per il mio portale di hotel e ho riscontrato un errore nel unire 2 tabelle con la funzione inner join, mi duplica i risultati di un "hotel" per quante offerte sono ad esso associate
le due tabelle sono cosi' composte:
la tabella "hotel" ha questi campi: idhotel,tipologia,nome,email,tel,statoinvece la tabella "offerte" e' composta cosi':
idofferte,prezzo,dal,al,idhotella pagina dei risultati è questa:
<link href="ischia_hotel.css" rel="stylesheet" type="text/css"> <? require_once ('conn.php'); $query = 'SELECT * FROM hotel INNER JOIN offerte ON hotel.idhotel=offerte.idhotel'; $result=mysql_query($query); while ($row=mysql_fetch_array($result)) : ?> <div align="center"><br /> <table class="ischia_box_interno" width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td><div align="center"><? echo' ' . $row['nome'] . ' ';?></div></td> </tr> <tr> <td><div align="center"><? echo' ' . $row['prezzo'] . ' ';?> EURO</div></td> </tr> </table> <br /> <? endwhile; @mysql_close(); ?> <br /> <br /> </div>Grazie
-
Ciao princo,
la query mi sembra corretta, è normale che se un hotel ha 2 offerte esce volte. -
Grazie per la risposta, ma non ce' un modo per far si che le determinate offerte associate all'id dell hotel escono ragruppate invece di duplicare sempre lo stesso hotel?
Grazie
Edit:
Ho scritto cosi' il codice, adesso invece di duplicarmi gli hotel mi mette tutte le offerte presenti nel database per ogni hotel presente mammamia che casino!!
<? require_once ('conn.php'); $query = 'SELECT * FROM hotel WHERE stato="aperto" ORDER by idhotel ASC'; $result=mysql_query($query); while ($row=mysql_fetch_array($result)) : ?> <br> <table align="center" class="ischia_box" border="0" cellpadding="0" cellspacing="0" width="520"> <tbody> <tr align="center"> <td height="60" width="90" class="tipologia"><div align="center"><? echo '<h2> ' . $row['tipologia'] . ' </h2>';?></div></td> <td width="452" height="60" align="left"><div align="left"><? echo '<h2><a class="b" href="hotel_ischia.php?id=' . $row['idhotel'] . '">' . $row['nome'] .'</h2></a></th>'; ?></div> </td> </tr> </tbody> </table> <table align="center" class="ischia_box_nolinea" border="0" cellpadding="0" cellspacing="0" width="520"> <tbody> <tr valign="top"> <td height="506"> <div class="ischia_txt4" align="center"> <p> </p> <table border="0" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr valign="top"> <td align="left" class="descrizione-hotel" height="143" width="280"><? echo ' ' . $row['descr_breve'] . ' ';?></td> <td width="220"><div align="center"><br> <? echo ' ' . $row['dal'] . ' ';?></div></td> </tr> </tbody> </table><br> <br> <strong class="ischia_supplementi_2"></strong><? require_once ('conn.php'); $query2 = 'SELECT * FROM hotel INNER JOIN offerte ON hotel.idhotel=offerte.idhotel'; $result2=mysql_query($query2); while ($row=mysql_fetch_array($result2)) : ?><br> <table class="ischia_box_interno" border="0" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td bgcolor="#59770e"> <div class="ischia_testo" align="center"><strong><? echo ' ' . $row['nome_offerta'] . ' ';?></strong></div></td> </tr> <tr valign="top"> <td height="178" width="500"> <br> <div align="center"> <table border="0" cellpadding="0" cellspacing="0" width="440"> <tbody> <tr> <td> <div align="center"><span class="data_hotel"><strong>Dal <? echo ' ' . $row['dal'] . ' ';?> Al <? echo ' ' . $row['al'] . ' ';?> </strong></span></div></td> </tr> <tr> <td> <div align="center"><span class="ischia-euro"><strong><? echo ' ' . $row['prezzo'] . ' ';?> EURO</strong></span><br> <span class="ischia_txt1"><strong>AL GIORNO PER </strong></span><span class="ischia_txt2"><strong> PERSONA </strong></span><span class="ischia_txt1"><strong>IN <? echo ' ' . $row['trattamento'] . ' ';?><br> <br> </strong></span></div></td> </tr> </tbody> </table> <br> <p> <span class="descrizione-hotel"> <strong class="ischia_txt3">INCLUSO NEL PREZZO:</strong> <? echo ' ' . $row['incluso'] . ' ';?></span></p> <p> </p> </div></td> </tr> </tbody> </table > <? endwhile; mysql_close($query); ?> <br> <br> <table border="0" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="25"><div align="center"><img src="ischia_images/ischia_hotel_mail_blu.jpg" alt="ischia prenotazioni" height="11" width="17"></div></td> <td class="menu" width="278"><a href="mailto:[email protected]" class="c"><? echo ' ' . $row['email'] . ' ';?></a></td> <td width="197"><div class="ischia-offerte" align="center"><strong><img src="ischia_images/schia-telefono.gif" height="13" width="20"> </strong><strong class="ischia-offerte"><? echo ' ' . $row['tel'] . ' ';?></strong></div></td> </tr> </tbody> </table> <p> </p> </div></td> </tr> </tbody> </table> <br> <br> <span class="ischia_txt4"> <? endwhile; mysql_close($query2); ?> </span></td> </tr> <tr> </tr> <tr> </tr> </tbody> </table> </div></td> </tr> </tbody></table> </div> <div align="center"></div> <div align="center"> <table class="ischia_boxsotto" border="0" cellpadding="0" cellspacing="0" width="740"> <tbody> <tr> [email protected]" class="ischia_vedisito">[email protected]</a></strong></span></div></td> </tr> </tbody> </table> <br> <br> </div> </body></html>Se vuoi vedere l'esempio scrivi il link del sito in firma aggiungendo /prova/
Grazie. -
Fai attenzione alle maiuscole e alla punteggiatura nei tuoi messaggi...
Per quanto riguarda il problema puoi usare GROUP BY per raggruppare. -
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi