PHP/MYSQL 通过 id "duplicate ids"查询

标签 php mysql

我试图从 3 个表中获取结果,但它重复 PART_ID 并一遍又一遍地显示相同的 ID。我该如何解决这个问题?

<?php
    $product_list = "";
    $sql = mysql_query("SELECT * FROM PART, PART_TYPE, RACK");
    $productCount = mysql_num_rows($sql);
    if ($productCount >0){

    while($row= mysql_fetch_array($sql)){
                 $id = $row["PART_ID"];
                 $PART_DESC = $row["PART_DESC"];
                 $SERIAL_NUM = $row["SERIAL_NUM"];
                 $RACK_NUM = $row["RACK_NUM"];
                 $PART_TYPE_ID = $row["PART_TYPE_ID"];
                 $PART_TYPE_DESC = $row["PART_TYPE_DESC"];
                 $product_list .= " <strong>PART_ID:</strong> $id -<strong>$PART_DESC</strong> -<strong>Product Type</strong> $SERIAL_NUM - <em><strong>RACK_NUM</strong> $RACK_NUM  - <em> <strong>PART_TYPE_ID</strong> $PART_TYPE_ID  - <em> <strong>PART_TYPE_DESC </strong> $PART_TYPE_DESC   - <em> &nbsp; &nbsp; &nbsp; <a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
    }

    }else 
        $product_list = "You have not items in the inventory yet"

?>

结果

PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R1S1 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R1S2 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R1S3 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R1S4 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R1S5 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R1S6 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R2S1 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R2S2 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R2S3 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R2S4 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R2S5 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R2S6 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R3S1 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R3S2 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete
PART_ID: 1001 -Power Mac G4 Desktop -Product Type XBO31 1WAJ3B - RACK_NUM R3S3 - PART_TYPE_ID 101 - PART_TYPE_DESC MAC -       edit • delete

enter image description here

最佳答案

如果您有任何一对多关系(看起来您确实有),则您的查询格式不正确。根据某种标准链接表格,如下所示:

SELECT * FROM PART
JOIN PART_TYPE on PART.part_type = PART_TYPE.ID
JOIN RACK ON PART.part_rack = RACK.ID

或者类似的东西。您想要告诉数据库您希望如何将这些表链接在一起。这是这样做的。

关于PHP/MYSQL 通过 id "duplicate ids"查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6169398/

相关文章:

php - 您如何在PHP中解析和处理HTML/XML?

java - 发送重置密码链接时保护用户 ID 的最佳方法是什么

PHP 变量未更新到 MYSQl 字段

php - Joomla 1.5/2.5/3 的 MySQL 注入(inject)漏洞

PHPunit - 错误

php - 写入 xml 文件时允许的内存大小为 67108864 字节耗尽(尝试分配 4459414 字节)

php - 准备声明问题发送加密信息

javascript - 分离 PHP 和 Javascript 代码的最佳方法

php - 使用 mysql 进行搜索查询和匹配排序

php - ssl 证书导致一个文件不工作