我有一个名为"prices“的表,其中有两个字段,其中我引用了另外两个名为”prodcut“和”suplier“的表。在“价格”表中,我引用了“prodcut”和“suplier”的"id“,我需要在php代码中获得两者的名称……到目前为止,我所拥有的是这样的,但我不知道如何让它工作:
$result = mysqli_query($conn, "SELECT * FROM prices") or die("Could not find");
if (mysqli_num_rows($result) > 0) {
while ($rr = mysqli_fetch_assoc($result)) {
$a = $rr['id'];
$b = $rr['date'];
$c = $rr['product'];
$d = $rr['suplier'];
$e = $rr['quantity'];
$f = $rr['packaging'];
$g = $rr['event'];
$h = $rr['price'];
$prov .="
<tr>
<td>".$b."</td>
<td><a href='productos-result.php?search=".$c."'>".$c."</a></td>
<td><a href='proveedores-result.php?search=".$d."'>".$d."</a></td>
<td>".$e."</td>
<td>".$g."</td>
<td>$".$h."</td>
</tr>";
}
}
我需要显示产品$c和suplier $d的名称,而不是id,我的表如下所示:
价格
id、日期、productid、suplierid、数量、事件、价格
产品
id、名称、描述
补充器
id、名称、描述
发布于 2018-06-14 05:32:59
更改您的查询以联接产品和供应商,并在列列表中将其名称别名为您想要的名称。
SELECT prices.id,
prices.date,
product.name product,
suplier.name suplier,
prices.quantity,
prices.packaging,
prices.event,
prices.price
FROM prices
INNER JOIN product
ON product.id = prices.productid
INNER JOIN suplier
ON suplier.id = prices.suplierid;
(如果您有没有供应商或产品的价格,但仍然希望显示它们(带有空名称),请将INNER JOIN
更改为LEFT JOIN
。)
https://stackoverflow.com/questions/50846315
复制相似问题