简体   繁体   English

使用连接查询两个表

[英]Using Joins to query two tables

I have two tables, "inventory" and "inventory_type". 我有两个表,“库存”和“inventory_type”。 The "inventory" table stores the name of it and the type, and the "inventory_type" table stores the type name such as RAM, CPU, etc. and the sort order. “inventory”表存储它的名称和类型,“inventory_type”表存储类型名称,如RAM,CPU等,以及排序顺序。 I have never used JOINS before and I am not sure which type to use, but none of them seem to work with the following code. 我之前从未使用过JOINS,我不确定使用哪种类型,但它们似乎都不适用于以下代码。

As a side question, with my code below, would "inventory" be the left table or would "inventory_type" be joined in on the left? 作为一个附带问题,使用下面的代码,“库存”是左表还是“inventory_type”会在左边加入?

function getInventoryOptions($db, $default_value, $datacenter)
{
    $query = "SELECT inventory.id, inventory.name, inventory_type.short_name
                FROM inventory LEFT JOIN inventory_type
                ON inventory.type = inventory_type.id WHERE inventory.datacenter = " . $datacenter . " ORDER BY inventory_type.sort_order ASC";

    $result = mysql_query($query, $db);

    echo '<option value="">None</option>';

    if ($result)
    {
        while($row = mysql_fetch_array($result))
        {
            $id = $row["inventory.id"];
            $name = $row["inventory.name"];
            $type = $row["inventory_type.short_name"];

            if ($default_value == $id)
            {
                echo '<option selected value="' . $id . '">' . $type . ": " . $name . '</option>';
            }
            else
            {
                echo '<option value="' . $id . '">' . $type . ": " . $name . '</option>';
            }
        }
    }
}

I think the problem lies here: 我认为问题出在这里:

$id = $row["inventory.id"];
$name = $row["inventory.name"];
$type = $row["inventory_type.short_name"];

try: 尝试:

$id = $row['id'];
$name = $row['name'];
$type = $row['short_name'];

* Note that the mysql extension is now deprecated and will be removed sometime in the future . *请注意,mysql扩展现在已弃用 ,将来某个时候将被删除 That's because it is ancient, full of bad practices and lacks some modern features. 那是因为它古老,充满了不良做法,缺乏一些现代特色。 Don't use it to write new code. 不要用它来编写新代码。 Use PDO or mysqli_* instead. 请改用PDOmysqli_ *

UPDATE UPDATE

Here's the equivalent of your function in mysqli: 这相当于你在mysqli中的函数:

function getInventoryOptions($db, $default_value, $datacenter)
{
     $query = "SELECT inventory.id, inventory.name, inventory_type.short_name
               FROM inventory LEFT JOIN inventory_type
               ON inventory.type = inventory_type.id WHERE inventory.datacenter = " . $datacenter . " ORDER BY inventory_type.sort_order ASC";

$link = mysqli_connect("[your_host]","[your_user]","[password]","[database]") or die("Error " . mysqli_error($link));



//execute the query.

$result = $link->query($query);

//display information:

while($row = mysqli_fecth_array($result)) {

        $id = $row['id'];
        $name = $row['name'];
        $type = $row['short_name'];

        if ($default_value == $id)
        {
            echo '<option selected value="' . $id . '">' . $type . ": " . $name . '</option>';
        }
        else
        {
            echo '<option value="' . $id . '">' . $type . ": " . $name . '</option>';
        }
} 

With the assumption that the inventory.type column is not null (please confirm that) there's no need for a LEFT JOIN . 假设inventory.typenot null (请确认),则不需要LEFT JOIN A INNER JOIN will do the trick. INNER JOIN将成功。

Since you only need an associative array, consider using mysql_fetch_assoc() instead of mysql_fetch_array() , or specify MYSQL_ASSOC as a second parameter. 由于您只需要一个关联数组,请考虑使用mysql_fetch_assoc()而不是mysql_fetch_array() ,或者将MYSQL_ASSOC指定为第二个参数。

Moreover, consider porting to mysqli or PDO altogether (see others' replies). 此外,考虑完全移植到mysqliPDO (参见其他人的回复)。

The returned associative array's keys are plain field names, without the table name part. 返回的关联数组的键是普通字段名称,没有表名部​​分。

Finally, for your side question, yes, inventory would be the left table, with all its records participating in the result regardless of whether or not there's a corresponding record in the inventory_type table. 最后,对于您的问题,是的, inventory将是左表,其所有记录都参与结果,无论inventory_type表中是否有相应的记录。

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

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