0
SELECT * from meets
 LEFT JOIN teams as hteam on meets.meet_hometeam=hteam.team_id 
 LEFT JOIN teams as ateam on meets.meet_awayteam=ateam.team_id 
 LEFT JOIN teams as altloc on (meets.meet_altloc=altloc.team_id and meets.meet_altloc!='')
 where meet_date between ($now+(4*86400)) and ($now+(5*86400) or meets.meet_id='2')

在 PHP 中:

$var = $queryvar->fetch_object();

当我称$var->ateam.team_town它只是将点视为连接而不是表的对象时,我遇到了问题。

4

1 回答 1

1

SELECT *JOINUNION查询时不要。相反,请具体说明您需要的列,如果不同表中的列具有相同的名称,则需要为它们分配别名。

除了区分具有相似名称的列的必要性之外,您还可以确定它们返回的顺序,并防止将来的架构更改添加您不希望自动拉入其中的列(如图像blob 数据什么的)。

SELECT
  /* specify columns from each table with aliases */
  hteam.team_id AS hometeam_id,
  ateam.team_id AS awayteam_id,
  hteam.team_town AS hometeam_town,
  ateam.team_town AS awayteam_town,
  ...
  ...
  etc...
FROM meets
 LEFT JOIN teams as hteam on meets.meet_hometeam=hteam.team_id 
 LEFT JOIN teams as ateam on meets.meet_awayteam=ateam.team_id 
 LEFT JOIN teams as altloc on (meets.meet_altloc=altloc.team_id and meets.meet_altloc!='')
 WHERE meet_date between ($now+(4*86400)) and ($now+(5*86400) or meets.meet_id='2')

然后在您的 PHP 中,只需通过您选择的别名调用它:$var->hometeam_town, $var->hometown_id, $var->awayteam_town等等...

于 2012-06-23T19:43:50.717 回答