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
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
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:

lupentino

Nuovo Utente
22 Mag 2008
10
0
1
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
 

lupentino

Nuovo Utente
22 Mag 2008
10
0
1
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
 

lupentino

Nuovo Utente
22 Mag 2008
10
0
1
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
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
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
 

lupentino

Nuovo Utente
22 Mag 2008
10
0
1
<?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: 62
  • Immagine 2022-11-26 152121.png
    Immagine 2022-11-26 152121.png
    696,3 KB · Visite: 71

lupentino

Nuovo Utente
22 Mag 2008
10
0
1
spero che cosi sia piu' chiaro e scusami
 

Allegati

  • Immagine 2022-11-26 152548.png
    Immagine 2022-11-26 152548.png
    73 KB · Visite: 64

lupentino

Nuovo Utente
22 Mag 2008
10
0
1
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
Autore Titolo Forum Risposte Data
K [php] Problema con inner join PHP 4
cosov Problema con INNER JOIN Classic ASP 1
O problema con dvr dahua xvr5116 IP Cam e Videosorveglianza 0
G Problema con Xampp Web Server 1
andrea barletta Problema con miniature comandi Photoshop 0
I problema con alice Posta Elettronica 0
N Problema con position absolute e overflow HTML e CSS 4
K [PHP] Problema con variabili concatenate. PHP 1
O problema con query PHP 4
I problema con 2 account Posta Elettronica 1
L problema collegamento file css con html HTML e CSS 1
E Problema accesso a file con app sviluppata con MIT APP INVENTOR 2 Sviluppo app per Android 0
M Problema con Try Catch PHP 0
Sergio Unia Problema con gli eventi del mouse su una data table: Javascript 2
T PROBLEMA CON SESSIONI PHP 3
T ALTRO PROBLEMA CON ARRAY PHP PHP 1
R problema con else PHP 0
T PROBLEMA CON ARRAY PHP 8
L problema con query select PHP 2
R Problema query con ricerca id numerico PHP 2
F Problema con risposta PHP 0
S problema con recupero dati tabella mysql PHP 2
Z Problema con il mio tp-l i nk Reti LAN e Wireless 1
L Problema RAM con Tomcat 8 Apache 0
napuleone problema con sort e asort PHP 4
Z Problema con INT MySQL PHP 1
Z Problema database MySQL con XAMPP PHP 0
M Problema con controllo form in real time jQuery 6
Z Problema di sincronizzazione PAYPAL con PHP PHP 1
G Problema con Get page PHP 4
P Problema con require once PHP 6
P Problema con i package Java 1
A Problema login con Safari PHP 14
F INDESIGN: problema esportazione esecutivo per la stampa con foto B/N Webdesign e Grafica 0
S problema con css bootstrap3 HTML e CSS 4
M .load() problema con caricamenti dinamici di js Javascript 0
G Problema con eccessiva nitidezza apertura Camera Raw Photoshop 0
G Problema ------- con Query PHP 1
G Problema con Query PHP 1
T problema con select dinamica con jquery Javascript 0
S Problema con spazi bianchi HTML e CSS 5
A PROBLEMA: insert mysqli con dati Tagsinput Presentati al Forum 0
Tommy03 Problema con z-index HTML e CSS 3
M Problema inserimento parole con apostrofo nel db PHP 5
C Problema con dati meteo xml XML 1
S Problema con infrarossi videocamera IP Cam e Videosorveglianza 1
V Problema con librerie allegro5 c++ C/C++ 1
M Problema con php per calcolo costo percentuale PHP 7
S Problema con mysqli_num_rows PHP 18
grgfede Problema javascript con aruba Javascript 1

Discussioni simili