0

学习如何为我的体育联盟网站加入 MySQL 表。这似乎是一个相对简单的问题。如果有人可以在这里帮助澄清我的思考过程,将不胜感激:

我有一个联赛日程页面,它为“test_league”MySQL 表中的每场比赛生成带有比赛信息的 HTML 表格行。其中一个 HTML 列旨在通过提取主队的位置数据来显示该比赛的位置,该位置数据存储在同一数据库的“user_schools”表中。

我确切地知道我想做什么,我似乎无法完全正确地使用语法。这是代码:

<?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);

?>

如果需要澄清,“home_team”位于“test_league”中,“team_name”位于“user_schools”中。

我已经看到了使用表连接的维恩图插图,但似乎无法执行这个看似简单的任务。非常欢迎您的建议!

4

4 回答 4

1

实际上,我没有执行此源代码,而是查看了 SQL。

似乎有些不对劲。试试这个查询。

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

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

WHERE 必须放在 JOIN 之后。并使用别名作为 A,B 来澄清。

于 2013-09-03T06:29:48.877 回答
1

您在代码中执行了三个单独SELECT的 s,这否定了 JOIN 的使用。你拥有你的代码的方式,你可以得到必要的信息,而不用做一个 JOIN,只需使用 WHERE。JOIN 允许您在一次调用中从多个相关表中选择列。使用 JOIN 在一个查询中获取所有信息的查询如下所示:

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;

我添加了表别名,以便您可以查看列的来源。请注意,最后两列来自user_schools表,而不是主test_league表。


然后,您的 PHP 代码将在这一行中进行更改,因为您现在拥有$row变量中的所有信息,您将其回显而不是$location$map

echo '<td><a href="' . $row['map'] . '">' . $row['football_location'] . '</a></td><td><a href="">' . $row['arbiter_id'] . '</a></td></tr>';
于 2013-09-03T07:02:04.177 回答
1
select us.football_location from user_schools us left join test_league tl on us.school_name = tl.home_team

它会给你所有的位置。

例如,如果您想要任何特定的条件,则将条件放在 where 子句中。

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'
于 2013-09-03T06:40:18.307 回答
1

试试这个加入:

tltest_leagueus的别名,是表的别名user_schools

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)

我在SQL Fiddle. 在 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>';
}
于 2013-09-03T07:12:58.513 回答