我有两个表,“inventory”和“inventory_type”。“inventory”表存储它的名称和类型,“inventory_type”表存储RAM、CPU等类型名称和排序顺序。我以前从未使用过 JOINS,我不确定要使用哪种类型,但它们似乎都不适用于以下代码。
作为一个附带问题,使用下面的代码,“inventory”是左表还是“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>';
}
}
}
}