学习如何为我的体育联盟网站加入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”。
我已经看过使用表连接的维恩图插图,但似乎无法执行这个看似简单的任务。非常欢迎您的建议!
答案 0 :(得分: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
;
JOIN之后必须放置的位置。并使用别名A,B来澄清。
答案 1 :(得分:1)
select us.football_location from user_schools us left join test_league tl on us.school_name = tl.home_team
它将为您提供所有位置。
如果你想在where子句中使用任何特定的put条件,例如。
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'
答案 2 :(得分:1)
您在代码中执行了三个单独的SELECT
,这取消了对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>';
答案 3 :(得分:1)
试试这个加入:
tl
是test_league
表的别名,us
是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
上添加了一个演示。 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>';
}