Problema con inner join

lupentino

Nuovo Utente
22 Mag 2008
10
0
1
Buonasera, a tutti ho un problema qui:

$sql = "SELECT * FROM fantaincontro INNER JOIN team ON team.id = fantaincontro.teamAway INNER JOIN team ON team.id = fantaincontro.teamHome WHERE fantaincontro.season LIKE '%$seasonFilter%' and fantaincontro.id LIKE '%$idnameFilter%' ORDER BY fantaincontro.id DESC";

credo che il problema si che richiamo 2 volte team.id?
grazie
 
ti riporto 2 possibili soluzioni (non provate),
vedi se ti vanno bene e soprattutto quale delle due é meglio per i risultati che vuoi

SQL:
SELECT * FROM fantaincontro f
INNER JOIN team t1 ON t1.id = f.teamAway
INNER JOIN team t2 ON t2.id = f.teamHome
WHERE f.season LIKE '%$seasonFilter%'
  AND f.id LIKE '%$idnameFilter%'
ORDER BY f.id DESC


SELECT * FROM fantaincontro f
INNER JOIN team t
   ON t.id = f.teamAway
  AND t.id = f.teamHome  ------------- potrebbe anche essere OR volendo
WHERE f.season LIKE '%$seasonFilter%'
  AND f.id LIKE '%$idnameFilter%'
ORDER BY f.id DESC
 
Ultima modifica:
grazie per aver risposto, allora il primo esempio funziona ma mi restituisce solo i risultati di teamAway..

all'interno della tabella devo usare i record di "name"

per esempio incontro: lupentinus - attimpuri ...
utilizzo:
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['name'] . "</td>";

ma mi esce lupentinus - lupentinus

il secondo errore mi da errore
 
scusami allora:
ho due tabelle fantaincontro dove ho 2 campid teamHome e teamAway hanno rispettivamente dati numerici esempio 5-10

l'altra tabelle è tem dove ho 1 campi name dove all'interno vengono riportati i nomi delle squadre (lupentinus e attimpuri)

5 e 10 corrispondono a due squadre Lupentinus e Attimpuri

entrambe le tabelle hanno in comune campi id per quanto riguarda la tabella team e appunto teamHome e teamAway..

in pratica quando vorre visualizzare l'incontro a schermo ma faccio il select solo sulla tabelle fantaincontro ovviamente mi escono i numero come risultato cioè 5-10
ma vorrei riportare i nomi delle squadra usando appunto, se giusto l'inner join

nel caso del primo esempio postato prima mi lupentinus-lupentinus

spero di essermi spiegato grazie
 
scusami allora:
ho due tabelle fantaincontro dove ho 2 campid teamHome e teamAway hanno rispettivamente dati numerici esempio 5-10

l'altra tabelle è tem dove ho 1 campi name dove all'interno vengono riportati i nomi delle squadre (lupentinus e attimpuri)

5 e 10 corrispondono a due squadre Lupentinus e Attimpuri

entrambe le tabelle hanno in comune campi id per quanto riguarda la tabella team e appunto teamHome e teamAway..

in pratica quando vorre visualizzare l'incontro a schermo ma faccio il select solo sulla tabelle fantaincontro ovviamente mi escono i numero come risultato cioè 5-10
ma vorrei riportare i nomi delle squadra usando appunto, se giusto l'inner join

nel caso del primo esempio postato prima mi lupentinus-lupentinus

spero di essermi spiegato grazie
dimenticavo l'altro campo che appartiene a team è name
 
meglio un esempio concreto che parole per descriverlo,
provo a postare un esempio scritto per ms sql, ma molto facile da capire,
se non raggiunge il risultato voluto per cortesia usalo per descrivere cosa vuoi ottenere
SQL:
DECLARE @team TABLE
(
  id        VARCHAR(20)
, team_des  VARCHAR(20)
);

INSERT INTO @team ( id, team_des )
SELECT 'team_1', 'team_1_des' UNION ALL
SELECT 'team_2', 'team_2_des' UNION ALL
SELECT 'team_3', 'team_3_des' UNION ALL
SELECT 'team_4', 'team_4_des' UNION ALL
SELECT 'team_5', 'team_5_des' UNION ALL
SELECT 'team_6', 'team_6_des';


DECLARE @fantaincontro TABLE
(
  id          VARCHAR(20)
, fant_des    VARCHAR(20)
, season      VARCHAR(20)
, teamAway    VARCHAR(20)
, teamHome    VARCHAR(20)
);

INSERT INTO @fantaincontro ( id, fant_des, season, teamAway, teamHome )
SELECT 'fant_01', 'fant_01_des', 'seas_1', 'team_1', 'team_2' UNION ALL
SELECT 'fant_02', 'fant_02_des', 'seas_1', 'team_3', 'team_4' UNION ALL
SELECT 'fant_03', 'fant_03_des', 'seas_1', 'team_5', 'team_6' UNION ALL
SELECT 'fant_04', 'fant_04_des', 'seas_1', 'team_6', 'team_5' UNION ALL
SELECT 'fant_05', 'fant_05_des', 'seas_1', 'team_4', 'team_3' UNION ALL
SELECT 'fant_06', 'fant_06_des', 'seas_1', 'team_2', 'team_1' UNION ALL
SELECT 'fant_11', 'fant_11_des', 'seas_2', 'team_1', 'team_2' UNION ALL
SELECT 'fant_12', 'fant_12_des', 'seas_2', 'team_3', 'team_4' UNION ALL
SELECT 'fant_13', 'fant_13_des', 'seas_2', 'team_5', 'team_6' UNION ALL
SELECT 'fant_14', 'fant_14_des', 'seas_2', 'team_6', 'team_5' UNION ALL
SELECT 'fant_15', 'fant_15_des', 'seas_2', 'team_4', 'team_3' UNION ALL
SELECT 'fant_16', 'fant_16_des', 'seas_2', 'team_2', 'team_1';


SELECT f.id
, f.fant_des
, f.season
, f.teamAway
, (select team_des from @team t where t.id = f.teamAway ) teamAway_des
, f.teamHome
, (select team_des from @team t where t.id = f.teamHome ) teamHome_des
 FROM @fantaincontro f
WHERE f.season LIKE '%seas_2%'
  AND f.id LIKE '%fant_%'
ORDER BY f.id

1669471631209.png
 
<?php
require_once "../config.php";
$idnameFilter = $_POST["first"];
$seasonFilter = $_POST["second"];

$sql = "SELECT * FROM fantaincontro f
INNER JOIN team t1 ON t1.id = f.teamAway
INNER JOIN team t2 ON t2.id = f.teamHome
WHERE f.season LIKE '%$seasonFilter%'
AND f.id LIKE '%$idnameFilter%'
ORDER BY f.id DESC";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
$index = 0;
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . ++$index . "</td>";
echo "<td>" . $row['IDFantagiornata'] . "</td>";
echo "<td>" . $row['GiornataDiA'] . "</td>";
echo "<td>" . $row['IDGirone'] . "</td>";
echo "<td>" . $row['season'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['name'] . "</td>";

echo "<td>";
echo "<div class='px-2' style='display:flex; justify-content:space-between;'>";
echo '<a href="update.php?id='. $row['id'] .'" class="mr-5" style="color : #007bff !important;" title="Modifica" data-toggle="tooltip"><span class="fa fa-pencil"></span></a>';
echo '<a href="delete.php?id='. $row['id'] .'" style="color : #007bff !important;" title="Cancella" data-toggle="tooltip"><span class="fa fa-trash"></span></a>';
echo "</div>";
echo "</td>";
echo "</tr>";
}
// Free result set
mysqli_free_result($result);
} else{
echo '<div class="alert alert-danger"><em>No records were found.</em></div>';
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
// Close connection
mysqli_close($link);
?>
 

Allegati

  • Immagine 2022-11-26 152041.png
    Immagine 2022-11-26 152041.png
    355,9 KB · Visite: 108
  • Immagine 2022-11-26 152121.png
    Immagine 2022-11-26 152121.png
    696,3 KB · Visite: 108
spero che cosi sia piu' chiaro e scusami
 

Allegati

  • Immagine 2022-11-26 152548.png
    Immagine 2022-11-26 152548.png
    73 KB · Visite: 102
infatti non e' quello che vorrei, nell'ultima tabella dovrebbero uscire le squadre (record) di teamAway, invece con l'inner join di sopra mi esce come da terza tabella
 

Discussioni simili