如何在MYSQLI查询中引用表点列表示法

时间:2012-06-23 13:51:04

标签: mysql select mysqli left-join

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')

$var = $queryvar->fetch_object();

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

1 个答案:

答案 0 :(得分:1)

执行SELECT *JOIN查询时,请勿UNION。相反,请具体说明所需的列,如果列在不同的表中具有相同的名称,则需要为它们指定别名。

除了区分具有相似名称的列的必要性之外,您还可以获得对其返回顺序的确定性以及对未来架构更改的保护,从而为添加列不希望被自动拉入此区域(如图像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等......