我从学校得到的这个mysql数据库存在一些问题。我获得了具有相同名称但数量不同的重复行。我认为这是因为'库存'包含与' sets'中名称相同的列。但我真的无法让它发挥作用。提前感谢您的帮助。
数据库原理图(瑞典语,但标题是英文。): http://weber.itn.liu.se/~stegu76/TNMK30-2016/legodatabasen.pdf
$setidquery = "SELECT inventory.Quantity, inventory.ItemID, inventory.ColorID, colors.Colorname, parts.Partname, sets.SetID, sets.Year, inventory.ItemtypeID FROM `inventory`, `parts`, `colors`, `sets` WHERE sets.SetID='$_COOKIE[setid]' AND inventory.ItemID=parts.PartID AND inventory.ColorID=colors.ColorID ORDER BY Partname ASC LIMIT 1000";
echo "<table class=\"table\">";
echo "<tr><th>Quantity:</th><th>Year:</th><th>Partname:</th><th>ItemID:</th><th>SetID</th><th>Image:</th></tr>";
while($row = mysqli_fetch_array($result)){
$prefix = "http://www.itn.liu.se/~stegu76/img.bricklink.com/";
$Year = $row['Year'];
$Quantity = $row['Quantity'];
$ItemID = $row['ItemID'];
$ColorID = $row['ColorID'];
$Partname = $row['Partname'];
$SetID = $row['SetID'];
$ItemtypeID = $row['ItemtypeID'];
$imagesearch = mysqli_query($conn, "SELECT * FROM `images` WHERE ItemTypeID = '$ItemtypeID' AND ItemID = '$ItemID' AND ColorID = '$ColorID' ");
$imageinfo = mysqli_fetch_array($imagesearch);
if($imageinfo['has_jpg']) {
$filename = "$ItemtypeID/$ColorID/$ItemID.jpg";
} else if($imageinfo['has_gif']) {
$filename = "$ItemtypeID/$ColorID/$ItemID.gif";
} else {
$filename = "noimage_small.png";
}
echo "<tr>
<td>$Year</td>
<td>$Quantity</td>
<td>$Partname</td>
<td>$ItemID</td>
<td>$SetID</td>
<td><img src=\"$prefix$filename\" alt=\"Part $ItemID\"/></td>
</tr>";
}
echo "</table>";
以下是为可读性而格式化的查询:
SELECT inventory.Quantity,
inventory.ItemID,
inventory.ColorID,
colors.Colorname,
parts.Partname,
sets.SetID, sets.Year,
inventory.ItemtypeID
FROM `inventory`, `parts`, `colors`, `sets`
WHERE sets.SetID='$_COOKIE[setid]'
AND inventory.ItemID=parts.PartID
AND inventory.ColorID=colors.ColorID
ORDER BY Partname ASC LIMIT 1000;
答案 0 :(得分:2)
问题是库存,零件和颜色没有连接到集合。
可以在where子句中进行连接,但是,正如Strawberry所说,我们现在更愿意在from子句中编写连接。我认为这使它们更容易阅读,这有助于发现缺少的连接。
这是你的select语句(格式化以帮助我看看发生了什么):
select
inventory.quantity,
inventory.itemid,
inventory.colorid,
colors.colorname,
parts.partname,
sets.setid,
sets.year,
inventory.itemtypeid
from
inventory,
parts,
colors,
sets
where
sets.setid='$_cookie[setid]' and
inventory.itemid = parts.partid and
inventory.colorid = colors.colorid
order by
partname asc
limit 1000;
这是加入版本。我已经在&#34;?&#34;你需要添加的东西:
select
inventory.quantity,
inventory.itemid,
inventory.colorid,
colors.colorname,
parts.partname,
sets.setid,
sets.year,
inventory.itemtypeid
from
inventory
join
parts
on inventory.itemid = parts.partid
join
colors,
on inventory.colorid = colors.colorid
join
sets
on set.? = ?.?
where
sets.setid='$_cookie[setid]'
order by
partname asc
limit 1000;
重要的是没有联系。我猜想有三组(无论如何都显示了每个例子)。因为您还没有说过如何连接设置查询会为每个集返回一次其他行,换句话说它会连接到每个集,因为您没有以任何方式限制连接。
如果您想了解有关SQL的更多信息,可以选择多种方法。我的网站是www.thedatastudio.net。