dtup3446 2013-07-08 15:35
浏览 41
已采纳

从多个表中检索记录MySQL

I have 2 tables, first is materials and second is category in my DB.

Materials table contains following:

___________________________________________________________________________
|id |type|color|supplier|name   |image      |category_id|material_price_30|
|___|____|_____|________|_______|___________|___________|_________________|
|1  |Gran|Black|        |Angola |angola.jpg |3          |100              |
|2  |Gran|Blue |        |Emerald|emerald.jpg|0          |120              |
|3  |Marb|Black|        |Galaxy |galaxy.jpg |8          |135              |
|4  |Marb|White|        |Visag  |visag.jpg  |1          |115              |
|5  |Quar|White|Sill Co |Orissa |orissa.jpg |5          |106              |

Category table contains following:

_____________________________
|id |name|thickness|price   |
|___|____|_________|________|
|1  |1   |30mm     |     169|
|2  |2   |30mm     |     244|
|3  |3   |30mm     |     280|
|4  |4   |30mm     |     316|
|5  |5   |30mm     |     347|
|6  |6   |30mm     |     411|
|7  |7   |30mm     |     496|
|8  |8   |30mm     |     544|
|9  |9   |30mm     |     612|
|10 |10  |30mm     |     689|
|11 |11  |30mm     |     775|

I have been using the following script to retrieve all images from MATERIALS, but now also need to add price from CATEGORY too, just do not know how. the category_id in MATERIALS should identify price for category from CATEGORY table.

Can you guys help me connect them 2 together?

here is my script which I have been using for a while now:

<?php
$samples = "SELECT * FROM materials WHERE materials.type = :cat and materials.supplier = '$supplier'";
$res = $db->prepare($samples);
$res->execute(array(':cat' => $category));
$count = $res->rowCount();
if($count > 0)
echo "
<section class=\"border mar_t_40\">
"; 
while ($row = $res -> fetch()){
    $postimggranite = $row[image];
    $postidgranite = $row[id];
    $folder = $row[type];
    $folder = strtolower($folder);
    $supplier = strtolower($supplier);
    $category_id = $row[category_id];

print<<<END
<span class="grid white_back mar_l_30">
<a class="fancybox" href="$img_path/$folder/$supplier/large/$postimggranite" rel="group[$postidgranite]" title="$row[name]"><img alt="$row[name]" src="$img_path/$folder/$supplier/small/$postimggranite" width="100" height="100">$row[name]</a>
</span>
END;

}
echo "<div class=\"clearfloat\"></div></section>";
?>
  • 写回答

1条回答 默认 最新

  • doujia6503 2013-07-08 15:38
    关注

    You should look into Sql Joins. They are what you need to join the results of two tables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办