1

这是我的第一篇文章,但我在这里阅读了很多年,并从中获得了很多重要信息。

请耐心等待我试图解释我的困境。我什至不确定如何给我的问题贴上标签,因为我真的不知道我正在尝试做什么的术语。我是 PHP/MySQL 的初学者(当您看到我的问题时,这将变得非常明显),但我渴望学习。

无论如何,这里是:

我有两个表,名为GamesTeams

mysql> describe games;

+-----------+---------------+------+-----+---------+----------------+
| Field     | Type          | Null | Key | Default | Extra          |
+-----------+---------------+------+-----+---------+----------------+
| id        | int(11)       | NO   | PRI | NULL    | auto_increment |
| homeTeam  | int(11)       | NO   |     | NULL    |                |
| awayTeam  | int(11)       | NO   |     | NULL    |                |
| homeScore | int(11)       | NO   |     | NULL    |                |
| awayScore | int(11)       | NO   |     | NULL    |                |
| homeOdds  | decimal(10,2) | NO   |     | NULL    |                |
| drawOdds  | decimal(10,2) | NO   |     | NULL    |                |
| awayOdds  | decimal(10,2) | NO   |     | NULL    |                |
| gameDate  | date          | NO   |     | NULL    |                |
+-----------+---------------+------+-----+---------+----------------+

mysql> describe teams;

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| team  | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

我想要这些字段Games.homeTeam并在和字段Games.awayTeam中查找它们的值。基本上应该在. 很简单。但是,我希望两者都在同一个 SQL 语句中查看表。Teams.idTeams.teamGames.homeTeamTeams.idGames.homeTeamGames.awayTeamTeams

我想也许这样的事情会起作用:

$result = mysql_query("SELECT * FROM Games g, Teams ht, Teams at where g.homeTeam = ht.id AND g.awayTeam = at.id ORDER BY g.gameDate") or die(mysql_error());

然后拿起行:

while($row = mysql_fetch_array($result)) { echo "Home Team: " . $row['ht.team'];}

那是不行的,因为我收到一个 mysql 错误,说没有名为ht.team.

我希望我在这里想要达到的目标相当清楚。我该怎么办?我假设我必须使用某种别名,但我无法理解 SQL 语句应该是什么样子才能完成我想要的。

4

2 回答 2

2

不要SELECT *与连接查询一起使用!始终明确说明列表中需要的SELECT列,并提供列别名:

SELECT 
  /* Only select the columns you actually need, and 
   * supply aliases to those with similar names
   */
  ht.team AS ht_team,
  ht.id AS ht_id,
  at.team AS at_team,
  at.id AS at_id
  g.homeScore,
  g.awayScore,
  g.homeOdds,
  g.drawOdds,
  g.awayOdds,
  g.gameDate
FROM 
  Games g, 
  Teams ht, 
  Teams at 
where g.homeTeam = ht.id AND g.awayTeam = at.id 
ORDER BY g.gameDate

在您的 PHP 中,通过它们的别名访问它们,例如:

echo "Home Team: {$row['ht_team']}";

顺便说一句,虽然您的隐式(逗号分隔的表和WHERE子句中的条件)连接方法会起作用,但通常建议使用显式连接。它是一种较新的语法,可以在需要时更容易LEFT JOIN扩展RIGHT JOIN

SELECT 
  ht.team AS ht_team,
  ht.id AS ht_id,
  at.team AS at_team,
  at.id AS at_id
  g.homeScore,
  g.awayScore,
  g.homeOdds,
  g.drawOdds,
  g.awayOdds,
  g.gameDate
FROM 
  /* Explicit JOIN syntax */
  Games g, 
  JOIN Teams ht ON g.homeTeam = ht.id 
  JOIN Teams at ON g.awayTeam = at.id 
ORDER BY g.gameDate
于 2012-09-27T13:08:41.367 回答
0

我认为问题是你只给表格一个别名,而不是列
如果在 Games 表格中没有名为 team 的列,你可以在循环中调用它

$row['团队'];

但是,如果两个表都具有与团队相同的名称列,那么您应该这样做。 select ht.team as htTeam , g.team as gTeam 来自 Teams ht inner join Games on ht.Id = .....

然后你可以达到每个团队任期

$row['htTeam']
and $row['gTeam'];

我认为习惯于决定表及其列的前缀

于 2012-09-27T13:09:21.880 回答