简体   繁体   English

显示来自SQL查询的同名列

[英]Display column from SQL query with same name

how can I display the name of both teams (lteam and vteam)? 如何显示两个团队(团队和团队)的名称? Query works now... This is a screenshot of the SQL results: 查询现在可用...这是SQL结果的屏幕截图: 在此处输入图片说明 http://prntscr.com/f9mkqh http://prntscr.com/f9mkqh

$sql = "
SELECT * 
  FROM fixtures 
  LEFT 
  JOIN teams AS a 
    ON fixtures.lteam = a.id
  LEFT 
  JOIN teams AS b 
    ON fixtures.vteam = b.id
 WHERE date_ko = '2017-05-19'
";
echo '<table>';
echo '<tbody>';

foreach($pdo->query($sql) as $row)
    {
    echo '<tr>';
    echo '<td>' . $row['lteam'] . '</td>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>-</td>';
    echo '<td>' . $row['vteam'] . '</td>';
    echo '<td>' . $row['b.name'] . '</td>';
    echo '</tr>';
    }

echo '</tbody>';
echo '</table>';

Thanks! 谢谢!

The SQL result header is somewhat confusing, and as I don't know fully how the tables and schemas are structured, I have to assume you are having trouble with getting the name attribute from the teams table, as this is joined in two times. SQL结果标头有些令人困惑,并且由于我不完全了解表和模式的结构,因此我不得不假设您在从teams表中获取name属性时遇到了麻烦,因为这两次被联接了。

You will need to select the columns explicitly and naming them something else for this to work. 您将需要显式选择列,然后为它们命名以使其起作用。 For example: 例如:

SELECT *, `a`.`name` as `team1name`, `b`.`name` as `team2name` FROM fixtures [...]

Now you should be able to grab the team names under their assigned aliases. 现在,您应该能够在为其分配的别名下获取团队名称。

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

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