如何获得MySQL中所有项目类型的最低价格和数量

时间:2017-03-14 14:47:16

标签: php mysql

所以我有两张形状像这样的桌子:

TABLE NAME: ab_skins_info
id | name | type | img | collection
***********************************
1  | Ex.. | 3    | 1   | Example
2  | Eg.. | 1    | 3   | Example 2
...

TABLE NAME: ab_shop
id | skin_id | owner | price | count
************************************
24 | 1       | Exa.. | 120   | 8
98 | 2       | Eop.. | 918   | 9
...

寻找输出:

id | name | type | img | collection | (lowest_price) | (count)
***************************************************************
1  | Ex.. | 3    | 1   | Example    | 120            | 1
2  | Eg.. | 1    | 3   | Example 2  | 918            | 1

基本上ab_shop存储当前销售额,id是皮肤ID本身,count是商店的自动增量和主键。 (我使用它是因为从库存到商店时数据从一个表移动到另一个表。)

我试图制作某种PHP商店以显示基本细节,另一页显示有关皮肤选择的更多高级细节。

查看获取所有皮肤信息,同时获得该skin_id的最低价格以及当前具有该skin_id的金额。我当前的代码是通过PHP,这涉及获取表的所有信息并加入它们,它们通过PHP不断检查和组织。我不认为这是最好的方法,因为商店往往充满了大约2000 - 5000皮肤,并且它使用大量带宽获取大量信息,这些信息将用于排序。有没有办法用MySQL命令做到这一点?

MySQL功能:

function getShopSkins($owner = false) {
    global $mysqli;
    $stmt = $mysqli->prepare("SELECT ab_shop.id, ab_shop.skin_id, ab_shop.price, ab_skins_info.name, ab_skins_info.img, ab_skins_info.collection, ab_skins_info.type
        FROM ab_shop
        JOIN ab_skins_info
            ON ab_shop.skin_id = ab_skins_info.id");
    $stmt->execute();
    $stmt->bind_result($id, $skin_id, $price, $name, $img, $collection, $type);
    $row = [];
    while ($stmt->fetch())
        $row[] = array('id' => $id, 'skin_id' => $skin_id, 'price' => $price, 'name' => $name, 'img' => $img, 'collection' => $collection, 'type' => $type);
    $stmt->close();
    return $row;
}

PHP函数:

if ($_a == "...") {
    $v_skinArray = getShopSkins();
    $t_skinTypeArray = [];
    foreach ($v_skinArray as $a) {
        $t_img = $a['img'];
        if (!isset($t_skinTypeArray[$t_img])) {
            $t_skinTypeArray[$t_img]['name'] = $a['name'];
            $t_skinTypeArray[$t_img]['skin_id'] = $a['skin_id'];
            $t_skinTypeArray[$t_img]['img'] = $a['img'];
            $t_skinTypeArray[$t_img]['collection'] = $a['collection'];
            $t_skinTypeArray[$t_img]['price'] = $a['price'];
            $t_skinTypeArray[$t_img]['type'] = $a['type'];
            $t_skinTypeArray[$t_img]['quantity'] = 1;
        } else if ($t_skinTypeArray[$t_img]['price'] > $a['price']) {
            $t_skinTypeArray[$t_img]['price'] = $a['price'];
            $t_skinTypeArray[$t_img]['quantity']++;
        } else {
            $t_skinTypeArray[$t_img]['quantity']++;
        }
    }
    requestResponse(true, "SUCCESS", $t_skinTypeArray); // Success!
    return;
}

0 个答案:

没有答案