问题描述
确定.我有两个表:product和product_sizes
Ok here it is. i have two tables: products and product_sizes
所以基本上我的产品表具有id(主键),名称(产品名称)和size_id(product_sizes中的外键)
so basically my product table has id(primary key),name(product name)and size_id(foreign key from product_sizes)
我的product_sizes表具有预定值:
my product_sizes table has predetermined values:
size_id name
------------------
1 1x1
2 2x2
3 3x3
在这里,我有一个有效的代码来显示产品表(使用while代码以html格式):
and here i have a working code displaying the product tables(in html using while code):
id name size_id
-----------------------------
1 product 1 1
2 product 2 2
3 product 3 1
我的问题是我想显示(用html表示)尺寸名称而不是其size_id,类似于以下示例:
my problem is that i want to display(in html) the size name rather than its size_id, something similar to these example:
id name size_id
-----------------------------
1 product 1 1x1
2 product 2 2x2
3 product 3 1x1
有人可以教我该怎么做吗?..我迫切需要它来完成我的最终项目.谢谢你.抱歉,我的英语不好.
can someone teach me how to do this?.. i need it urgently for my final project. thank you. sorry for my explanation im not good in english.
推荐答案
使用JOIN:
SELECT p.id,
p.name,
ps.name AS size_name
FROM PRODUCT p
JOIN PRODUCT_SIZES ps ON ps.size_id = p.size_id
请参见此视觉链接各种JOIN的表示形式.
<? include ("conn.php");
$sql = "SELECT p.id,
p.name,
ps.name AS size_name
FROM PRODUCT p
JOIN PRODUCT_SIZES ps ON ps.size_id = p.size_id";
$result = mysql_query($sql, $connection) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td><? echo $row['id']; ?></td>
<td><? echo $row['name']; ?></td>
<td><? echo $row['size_name']; ?></td>
</tr>
<? } ?>
这篇关于内部连接mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!