简体   繁体   English

协助MySQL左外部联接并从同一键中区分查询结果

[英]Assistance with MySQL left outer join and differentiating query results from same key

I am trying to learn about SQL joins and trying to apply them to an application I am building. 我正在尝试了解SQL联接,并尝试将它们应用于正在构建的应用程序。 I am doing a query to find a "game record" on a schedule based on a specific game id. 我正在查询以根据特定的游戏ID在时间表上查找“游戏记录”。 But on this game record; 但是在这个游戏记录上; for the "h_team" and the "v_team"; 用于“ h_team”和“ v_team”; only the ids of the teams are on the game record. 游戏记录中只有团队的ID。 And so what I want to do is join the "teams" table and look up the two different team_names of the "h_team" and "v_team". 因此,我想做的就是加入“团队”表,并查找“ h_team”和“ v_team”的两个不同的team_name。 I have it also pull in a "division name" as well using a join since only the division id is stored on the game record. 由于只有分区ID存储在游戏记录中,因此我也使用联接来引入“分区名称”。 I have gotten this all to work fine; 我已经做好了一切; except I do not know how to get the results separately for the "team_name" for h_team and v_team. 除了我不知道如何分别获取h_team和v_team的“ team_name”的结果。 Basically the key for each one is just "team_name"; 基本上,每个密钥都是“ team_name”; I will paste in my code and then explain further: 我将粘贴我的代码,然后进一步解释:

$array_game_id6=32;


$sql = "SELECT * FROM playoff_schedule LEFT OUTER JOIN teams on  playoff_schedule.h_team = teams.team_id || playoff_schedule.v_team =   teams.team_id LEFT OUTER JOIN playoff_divisions on playoff_schedule.po_div_id  = playoff_divisions.po_div_id WHERE tom_game_id=$array_game_id6";
foreach ($dbh->query($sql) as $resultsg39)
{
  $h_team=$resultsg39[h_team];
  $v_team=$resultsg39[v_team];
  $po_div_id=$resultsg39[po_div_id];
  $round=$resultsg39[round];
  $game_id=$resultsg39[game_id];
  $date=$resultsg39[date];
  $timestamp=$resultsg39[timestamp];
  $h_score=$resultsg39[h_score];
  $v_score=$resultsg39[v_score];
  $tom_game_id=$resultsg39[tom_game_id];

$h_name=$resultsg39[team_name];
$div_name=$resultsg39[playoff_name];
}

the problem comes in when i am trying to get the results of the query and store them all in the different variables… 当我尝试获取查询结果并将它们全部存储在不同的变量中时,问题就来了……

the last two "$h_name" and "$div_name" are being pulled from the JOINs all the prior ones are on the game record itself… 最后两个“ $ h_name”和“ $ div_name”从JOIN中被拉出,所有先前的都在游戏记录本身中…

what I want to do is store both the names from "v_team" and "h_team" in the respective variables $h_name and $v_name; 我要做的是将“ v_team”和“ h_team”的名称都存储在各自的变量$ h_name和$ v_name中;

I have it storing the $h_name no problem; 我有它存储$ h_name没问题; but i do not know how to make it store both $h_name and $v_name separately as they are both values in the column "team_name" from "teams" table. 但我不知道如何使其分别存储$ h_name和$ v_name,因为它们都是“ teams”表中“ team_name”列中的值。 So I just need to somehow make it so when i get my results it can tell the difference between the two different "team_names" and I can store them in the two different variables… 所以我只需要以某种方式做到这一点,以便当我得到结果时,它可以告诉两个不同的“ team_names”之间的区别,并且可以将它们存储在两个不同的变量中……

If this is not clear please let me know. 如果不清楚,请告诉我。

Thanks! 谢谢!

***** UPDATE 10:49pm EST 2/5/2015 have made some progress on this but my query is not working; *****更新时间:美国东部标准时间2015年2月5日10:49 pm取得了一些进展,但我的查询无法正常工作; I think it is a problem with the aliases and such are not right; 我认为别名有问题,这是不对的。 here is my non-working query as it is right now: 这是我目前无法使用的查询:

$sth = $dbh->prepare("SELECT home_team.team_name as home_team_name,  visiting_team.team_name as visiting_team_name, 
h_team, v_team, po_div_id, round, game_id, date, timestamp, h_score,   v_score, tom_game_id, playoff_name FROM playoff_schedule 
LEFT OUTER JOIN teams as home_team on playoff_schedule.h_team =   teams.team_id 
LEFT OUTER JOIN teams as visiting_team on playoff_schedule.v_team =  teams.team_id 
LEFT OUTER JOIN playoff_divisions on playoff_schedule.po_div_id =  playoff_divisions.po_div_id 
WHERE tom_game_id=$array_game_id6");

$sth->execute();

$article_list = $sth->fetchAll(PDO::FETCH_ASSOC);

foreach ($article_list as $row => $link) {
$h_team=$link['h_team'];
$v_team=$link['v_team'];
$po_div_id=$link['po_div_id'];
$round=$link['round'];
}

if anyone can spot a problem with my new query I would really appreciate it! 如果有人能发现我的新查询有问题,我将不胜感激!

I think what you are trying to do is: 我认为您正在尝试做的是:

  select home_team.team_name as home_team_name,
         visiting_team.team_name as visiting_team_name
    from playoff_schedule
    join team as home_team on playoff_schedule.h_team = teams.team_id
    join team as visiting_team on playoff_schedule.v_team = teams.team_id

You can join to the same table as many times as you want to. 您可以根据需要多次加入同一张表。 In this case, it makes sense, because you really are trying to get two different bits of information. 在这种情况下,这很有意义,因为您实际上是在尝试获取两个不同的信息。

Based on your last edit, the following query appears to work: 根据您的上一次编辑,以下查询似乎有效:

SELECT home_team.team_name AS home_team_name,
       visiting_team.team_name AS visiting_team_name,
       h_team,
       v_team,
       playoff_schedule.po_div_id,
       round,
       game_id,
       date,
       timestamp,
       h_score,
       v_score,
       tom_game_id,
       playoff_name
  FROM playoff_schedule
       LEFT OUTER JOIN teams AS home_team
          ON playoff_schedule.h_team = home_team.team_id
       LEFT OUTER JOIN teams AS visiting_team
          ON playoff_schedule.v_team = visiting_team.team_id
       LEFT OUTER JOIN playoff_divisions
          ON playoff_schedule.po_div_id = playoff_divisions.po_div_id
  WHERE tom_game_id=$array_game_id6

You can check the query and the schema at: SQLFiddle 您可以在以下位置检查查询和架构: SQLFiddle

A couple of thing that might be happening: 可能会发生的几件事:

  • Is the query itself running? 查询本身在运行吗?
  • What happens if you run the query in a mySQL client? 如果在mySQL客户端中运行查询会怎样?
  • Are there any PHP errors in your log? 您的日志中是否有任何PHP错误?
  • Could you post the schema itself? 您可以发布架构本身吗?
  • Is $array_game_id6 actually an array of values? $ array_game_id6实际上是一个值数组吗? In that case, you need to use "in" as opposed to "=" in your where clause. 在这种情况下,您需要在where子句中使用“ in”而不是“ =”。

With regard to your updated query, I think the main thing you are missing is using the aliases in your JOIN conditions. 关于更新的查询,我认为您缺少的主要内容是在JOIN条件中使用别名。 You should keep your table aliases consistent throughout your query. 您应在整个查询中保持表别名的一致性。 Also, IMO its better to keep table aliases short so they are easier to read: 另外,IMO最好使表别名简短,以便于阅读:

So applying those things to your query: 因此,将这些内容应用于您的查询:

SELECT h.team_name as h_team_name, v.team_name as v_team_name, s.h_team, s.v_team, s.po_div_id, s.round, s.game_id, s.date, s.timestamp, s.h_score, s.v_score, s.tom_game_id, s.playoff_name 
FROM playoff_schedule s
LEFT OUTER JOIN teams h ON (
    s.h_team = h.team_id
) 
LEFT OUTER JOIN teams as v ON (
    s.v_team = v.team_id 
)
LEFT OUTER JOIN playoff_divisions d ON (
    s.po_div_id = d.po_div_id
)
WHERE s.tom_game_id = ?

Now I'm not 100% sure of your schema so I may have referenced some of the columns to the wrong table but you should be able to sort that out. 现在,我不确定您的模式是否100%正确,因此我可能已经将某些列引用到错误的表中,但是您应该可以对它们进行整理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM