【问题标题】:Get info from two tables in database where id match an array?从数据库中 id 与数组匹配的两个表中获取信息?
【发布时间】:2013-10-10 19:20:31
【问题描述】:

我有两张表,一张是存储不同价格的价格:

prices

appid      de      us      ru      nl      gb
29382      899     999     1299     899    699
48371     1299    1599     1899    1299    999
58193      699     899      999     899    599 

另外一个表是游戏,里面存储了游戏的各种信息:

games

appid      title      releasedate      controler      language
29382     title 1     1358197200           1             en
48371     title 2     1329858000           0             en
58193     title 3     1201554000           1             en

如您所见,两个表具有相同的 appid,我需要将其与 json 文件中的外部 id 匹配

最后我有一个 json 文件,如下所示:

{
"response": {
    "games": [
        {
            "appid": 58193,
            "playtime_forever": 0
        },
        {
            "appid": 29382,
            "playtime_forever": 0
        },
        {
            "appid": 48371,
            "playtime_forever": 151
        }
    ]

}
}

现在,如果这只是基于 json 字段从一个表中提取信息,我会知道该怎么做,但对我来说这似乎有点复杂。我尝试了各种解决方案,但从未找到有效的解决方案。

我现在的最终代码是这样的:

$listusergames = json_decode(file_get_contents('http://path_to_file.json'));

    foreach ($listusergames->response->games as $game) {
        $gameid = $game->appid;

        $querygamename = mysqli_query($conn, "SELECT * FROM games WHERE appid='$gameid'");
        $rowgame = mysqli_fetch_array($querygamename);
        if($rowgame[0] > 1 ) {
        echo 'Your game: ' . $rowgame['title'] . ' is worth $' . $rowgame['us'] . ' and game id is - '. $gameid . '<br/>';
        }
    }

在这段代码中,$rowgame['us'] 现在无效,因为我不知道如何根据 json 字段从两个表中提取数据。

在我现在拥有的解决方案中,每个 appid 都是从 json 文件中提取的,然后我根据 json 文件中的 appid 值从游戏表中选择所有值,并使用 if($rowgame[0] > 1 ) 因为 otherwite它将为数据库中不存在的 json 文件中的每个 appid 回显空行。

但我不知道如何选择两个表并从一个表中显示游戏标题以及从另一个表中显示不同的价格并将它们与 json 文件中的 appid 匹配,如果数据库中不存在则跳过该 appid。

编辑:一个更快速的问题,因为 mysql_query 在 foreach 循环中不会消耗太多资源,因为查询将为每个 $game 执行,所以如果我有 500 场比赛,我将为每个循环有 500 个查询,并制作我的网站很慢,还是我错了?

【问题讨论】:

  • us 字段仅存在于您的 prices 表中,您不会在查询中从中获取该表。您必须一起JOIN 表。
  • 你不会写JOIN?
  • Barmar 我还在学习,我尝试了 JOIN 或 UNION 的各种解决方案,但没有运气。我现在试试 Set Sail Media 解决方案看看。

标签: php mysql sql database json


【解决方案1】:

更新查询以加入两个表:

SELECT g.*, p.* FROM games g 
LEFT JOIN prices p on (g.appid = p.appid) 
WHERE g.appid='$gameid'

传统上,您可以implode() 将 JSON 结果中的数组转换为一个字符串,并使用 WHERE/IN SQL 语句。但是,因为您正在查看对象的 appid 参数,而不是一个简单的数组,所以如果不付出一点额外的努力,您就无法 implode()

您可以通过 JSON 结果foreach() 来构建您的查询,并只执行一条 MySQL 语句,这将节省一些开销。

完整更新的代码:

// Grab JSON list
$listusergames = json_decode(file_get_contents('http://path_to_file.json'));

// Setup new array
$games = array();

// Add member to array for each game's appid
// Resulting array looks like ( [0]=>'58193', [1]=>'29382', [2]=>'48371 )

foreach ($listusergames->response->games as $game) {
    $games[] = $game->appid;
}

// Implode games array into a comma-separated string like: "58193,29382,48371"
$games_list = implode( ',' , $games );

// Updated SQL statement using WHERE/IN list
$querygamename = mysqli_query($conn, "SELECT g.*, p.* FROM games g 
LEFT JOIN prices p on (g.appid = p.appid) 
WHERE g.appid IN ($games_list) ");

// Loop through result set
while ( $rowgame = mysqli_fetch_array($querygamename) ){
    if($rowgame[0] > 1 ) {
        echo 'Your game: ' . $rowgame['title'] . ' is worth $' . $rowgame['us'] . ' and game id is - '. $gameid . '<br/>';
    }
}

【讨论】:

  • 您编辑答案的速度太快了:P 您的答案效果很好,无论是旧的还是带有数组的,只是一个快速的问题,哪个会更快且消耗资源更少?如果我有 500 场比赛,恐怕我的 foreach 循环会查询 500 次?
  • 哈哈,对不起! :) 新的 foreach() 循环的美妙之处在于,无论它循环了多少 JSON 结果,它最终都只是执行了一个 MySQL 查询。
  • 再次抱歉,您的意思是在您使用数组或我的解决方案的新答案中“最终只执行一个 MySQL 查询”?
  • 在我发布的上述答案中,只有一个 MySQL 查询。在您的 JSON 结果中,每个项目都有一个。
猜你喜欢
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
相关资源
最近更新 更多