简体   繁体   中英

PHP/MySQL Join Syntax: Comparing Two Columns in Separate Tables

Learning how to join MySQL tables for my sports league website. This seems like a relatively simple question. Would greatly appreciate it if someone could help clarify my thought process here:

I have a league schedule page that generates HTML table rows with game info for every game in the "test_league" MySQL table. One of the HTML columns is intended to display the location for that game by pulling the location data for the home team, which is stored in the "user_schools" table in the same database.

I know exactly what I want to do, I just can't seem to get the syntax quite right. Here's the code:

<?php

// Connect to the database:
require ('../mysqli_connect.php');

// Make the query for games from the schedule database:
$q = "SELECT game_date, game_time, away_team, home_team, home_score, away_score, arbiter_id FROM test_league";
$r = mysqli_query($db, $q);

// Make the query to determine where the games will be played:
$location = mysqli_query($db, "SELECT football_location FROM user_schools WHERE home_team=school_name INNER JOIN test_league");
$map = mysqli_query($db, "SELECT football_map FROM user_schools WHERE home_team=school_name INNER JOIN test_league");

// Declare two variables to help determine the background color of each row:
$i = 0;
$bgcolor = array('row1', 'row2');

// Begin function to print each game as a row:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

    echo '<tr class="' . $bgcolor[$i++ % 2] .'"><td>' . $row['game_date'] . '</td><td>' . $row['game_time'] . '</td><td class="alignleft"><a href="">' . $row['away_team'] . '</a> vs<br><a href="">' . $row['home_team'] . '</a></td>';

    // Determine if the score has been reported:
    if ($row['home_score'] == '0' && $row['away_score'] == '0') {
        echo '<td><a href=""><img src="images/report_icon.png" alt="Report Score" /></a></td>';
    } else {
        echo '<td>' . $row['home_score'] . '<br>' . $row['away_score'] . '</td>';
    }

    echo '<td><a href="' . $map . '">' . $location . '</a></td><td><a href="">' . $row['arbiter_id'] . '</a></td></tr>';

}

mysqli_free_result ($r);

mysqli_close($db);

?>

If clarification is needed, "home_team" is located in "test_league" and "team_name" is located in "user_schools".

I've seen the Venn diagram illustrations for using table joins, but can't quite seem to execute this seemingly straightforward task. Your advice is most welcome!

Actually, i didn't execute this source code but review the SQL.

something's seems wrong. try this query.

SELECT football_location 
FROM user_schools A
INNER JOIN test_league B
WHERE A.home_team=A.school_name 
;

and

SELECT football_map 
FROM user_schools A
INNER JOIN test_league B
WHERE A.home_team=A.school_name 
;

WHERE must be placed after JOIN. and use alias as A,B to clarify.

select us.football_location from user_schools us left join test_league tl on us.school_name = tl.home_team

It will get you all the locations.

If you want any specific then put condition in where clause, forexample.

select us.football_location from user_schools us left join test_league tl on us.school_name = tl.home_team where us.id = 'id here'

You are doing three separate SELECT s in your code, which negates the use of a JOIN. The way you have your code you can get the requisite information without doing a JOIN at all, just use the WHERE. The JOIN allows you to select columns from multiple related tables in a single call. The query to get all the information in one query using the JOIN would look like this:

SELECT tl.game_date, tl.game_time, tl.away_team, tl.home_team, tl.home_score,tl.away_score, tl.arbiter_id, us.football_location, us.football_map
FROM test_league tl
    INNER JOIN user_schools us ON (tl.home_team = us.school_name)
ORDER BY tl.game_date, tl.game_time, tl.home_team, tl.away_team;

I added table aliases so you can see where the columns are coming from. Notice that the last two columns come from the user_schools table and not the main test_league table.


Your PHP code would then have a change in this line, since you now have all the information in the $row variable, you echo that out instead of $location and $map :

echo '<td><a href="' . $row['map'] . '">' . $row['football_location'] . '</a></td><td><a href="">' . $row['arbiter_id'] . '</a></td></tr>';

try this join:

tl is alias for test_league table and us is alias for user_schools table.

SELECT
    tl.*,
    us.football_location,
    us.football_map
FROM
    test_league tl
LEFT OUTER JOIN user_schools us ON (tl.home_team = us.school_name)

I added a demo on SQL Fiddle . See the results on SQL Fiddle

$q = "SELECT
        tl.*,
        us.football_location,
        us.football_map
    FROM
        test_league tl
    LEFT OUTER JOIN user_schools us ON (tl.home_team = us.school_name)";
$r = mysqli_query($db, $q);

$i = 0;
$bgcolor = array('row1', 'row2');

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    echo '<tr class="' . $bgcolor[$i++ % 2] .'"><td>' . $row['game_date'] . '</td><td>' . $row['game_time'] . '</td><td class="alignleft"><a href="">' . $row['away_team'] . '</a> vs<br><a href="">' . $row['home_team'] . '</a></td>';
    if ($row['home_score'] == '0' && $row['away_score'] == '0') {
        echo '<td><a href=""><img src="images/report_icon.png" alt="Report Score" /></a></td>';
    } else {
        echo '<td>' . $row['home_score'] . '<br>' . $row['away_score'] . '</td>';
    }
    echo '<td><a href="' . $row['football_map'] . '">' . $row['football_location'] . '</a></td><td><a href="">' . $row['arbiter_id'] . '</a></td></tr>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM