简体   繁体   English

从多个表中检索记录MySQL

[英]Retrieving Records from Multiple Tables MySQL

I have 2 tables, first is materials and second is category in my DB. 我有2个表,第一个是材料,第二个是我的数据库中的类别。

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. 我一直在使用以下脚本从MATERIALS中检索所有图像,但现在也需要从CATEGORY添加价格,只是不知道如何。 the category_id in MATERIALS should identify price for category from CATEGORY table. MATERIALS中的category_id应该从CATEGORY表中识别类别的价格。

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>";
?>

You should look into Sql Joins . 你应该看一下Sql Joins They are what you need to join the results of two tables. 它们是您加入两个表的结果所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM