我如何查询和查看多个表中的记录

时间:2014-02-02 04:28:16

标签: php mysql

这里是我的代码,我从三个表中查询,它查询并输出记录但我的问题示例如果我有2条记录它会多次出现

代码:

$sql = mysql_query("SELECT * FROM tbl_product, product_category, product_subcategory ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
  while($row = mysql_fetch_array($sql)){ 
    $id = $row["product_id"];
    $product_name = $row["product_name"];
    $product_category = $row["product_category"];
    $unit_price = $row["unit_price"];
    $quantity = $row["quantity"];
    $product_subcategory = $row["product_subcategory"];
    $size = $row["size"];
    $color = $row["color"];
    $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

    $inventory_list .= "Product ID: $id
       -- product_name: $product_name
       -- product category: $product_category
       -- product subcategory: $product_subcategory
       -- size: $size
       -- color: $color 
       -- unit price: $unit_price 
       -- quantity: $quantity 
       -- <a href='inventory_edit.php?pid=$id'>edit</a>
       -- <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
在我回应之后, 输出:(这个应该只有2行)

Product ID: 9 -- product_name: black shoes -- product category: asd -- product subcategory: formal shoe -- size: 5 -- color: red -- unit price: 12.00 -- quantity: 32 -- edit -- delete
Product ID: 9 -- product_name: black shoes -- product category: asd -- product subcategory: asd2 -- size: 12332 -- color: asdas -- unit price: 12.00 -- quantity: 32 -- edit -- delete
Product ID: 10 -- product_name: red shoe -- product category: asd -- product subcategory: formal shoe -- size: 5 -- color: red -- unit price: 343.00 -- quantity: 5345 -- edit -- delete
Product ID: 10 -- product_name: red shoe -- product category: asd -- product subcategory: asd2 -- size: 12332 -- color: asdas -- unit price: 343.00 -- quantity: 5345 -- edit -- delete
Product ID: 9 -- product_name: black shoes -- product category: men shoe -- product subcategory: formal shoe -- size: 5 -- color: red -- unit price: 12.00 -- quantity: 32 -- edit -- delete
Product ID: 9 -- product_name: black shoes -- product category: men shoe -- product subcategory: asd2 -- size: 12332 -- color: asdas -- unit price: 12.00 -- quantity: 32 -- edit -- delete
Product ID: 10 -- product_name: red shoe -- product category: men shoe -- product subcategory: formal shoe -- size: 5 -- color: red -- unit price: 343.00 -- quantity: 5345 -- edit -- delete
Product ID: 10 -- product_name: red shoe -- product category: men shoe -- product subcategory: asd2 -- size: 12332 -- color: asdas -- unit price: 343.00 -- quantity: 5345 -- edit -- delete

1 个答案:

答案 0 :(得分:0)

我不确定您的列名,但您希望查询看起来像这样:

SELECT * FROM tbl_product
JOIN product_category ON tbl_product.category_id = product_category.id
JOIN product_subcategory ON tbl_product.subcategory_id = product_subtegory.id
ORDER BY date_added DESC

这是写它的明确方式。隐含的方式是:

SELECT * FROM tbl_product, product_category, product_subcategory
WHERE tbl_product.category_id = product_category.id AND tbl_product.subcategory_id = product_subtegory.id
ORDER BY date_added DESC

您的版本会产生所有可能的组合。您必须使用ON或WHERE子句来排除您不想要的组合。或者换句话说,告诉它如何将行连接在一起。