douqian5920 2016-04-15 12:48
浏览 34
已采纳

从外键表中检索字段

I have these two tables and I want to get some values from the second table for two foreign key in the first table.

Team table:

ID   | Name          | No_Players | Country  | F_Year | Team_Logo
-----+---------------+------------+----------+--------+----------------------
1    | Real Madrid   |  22        | Spain    | 1940   | Http://Xyz.jpg  
2    | Arsenal       |  22        | England  | 1950   | Http://XXYYZZ.jpg  
3    | FCB.Barcelona |  22        | Spain    | 1960   | Http://YYZZz.jpg  
4    | Bayern Meunekh|  22        | Germany  | 1940   | Http://GGG.jpg     

Matches table:

ID | Date        |First_Team     | Second_Team | M_Country |M_City   | M_Period | Result
------------------------------------------------------------------------------------------
1  |15-02-2016   | Real Madrid   | Arsenal     | Spain     |  Madrid | 120      | 1-1
2  |19-03-2016   | FCB.Barecolna | Madrid      | Spain     |  Madrid | 90       | 4-5

And I want to Get the Team_Logo For each team in the matches table in the json form in this PHP Script. I've tried to get teams logo but unfortunately the query is invalid.

If anyone can help me please?

<?php
require ('config.php');
$conn = mysqli_connect($servername, $username, $password, $db);
$query = "select * from matches,team where matches.first_team=team.Name OR matches.second_team=team.Name";
$result = mysqli_query($conn, $query);
$rows = array();
echo mysqli_error($conn);

while($row = mysqli_fetch_assoc($result)) {
    $rows[]=$row;
}

echo json_encode($rows);
?> 
  • 写回答

3条回答 默认 最新

  • douxin1884 2016-04-15 12:57
    关注
    SELECT
        matches.*,
        t1.Team_Logo AS logo1,
        t2.Team_Logo AS logo2
    FROM matches
    JOIN team AS t1
        ON t1.Name = matches.First_Team
    JOIN team As t2
        ON t2.Name = matches.Second_Team
    

    Also note that you should not select * because the Id columns from the team tables will override the Id column of the matches table. That's why you should include only columns you like to select.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么