duanchu9914 2013-03-02 00:26
浏览 74
已采纳

使用phpmyadmin,我如何在数据库中存储一系列数据? (利用多对多的关系)

I need to make a database containing information on pharmaceutical drugs.

  • Column one is the unique identifier field with auto-increment. e.g., "1"
  • Column two contains the name of the drug. e.g., "Tramadol"
  • Column three contains a list of weights. e.g., "30g , 40g , 50g , 60g"

Using a php while loop and an array, I could echo this data using the following method:

while ($row = mysql_fetch_array($result)) {
   echo "Drug Name: {$row[1]} Drug Weights: {$row[2]}"
}

While this would work, I know it is definitely not the best way. The list of weights would have to be a varchar, rather than actual individual integers. What would be the best way to store and echo a range of information for a single product, as seen in column three?

  • 写回答

2条回答 默认 最新

  • dpus81500574 2013-03-02 00:53
    关注

    This is actually a many-to-one relationship, as you have multiple dosage amounts in relation to a single drug: http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/

    As for your problem, this depends on the context of how you want to use the data. If the dosage amounts are meant to be used as reference to the various products offered at the pharmacy (i.e. a small bottle of Tramadol and a large bottle), then I would probably just add a new row for every dosage amount, as you will probably have a limited set of values to choose from in the first place, and it's more representative of what a store's inventory would look like. You could then iterate through it with nested loops like this:

    $drugs = array()
    while ($row = mysql_fetch_array($result)) {
        $drugs[$row['name']][] = $row['dosage_amount']
    }
    foreach ($drugs AS $drug => $dosages) {
        echo '<h2>'. htmlentities($drug, ENT_COMPAT, 'UTF-8') .'</h2>';
        foreach ($dosages AS $dosage) {
            echo '<br><em>'. htmlentities($dosage, ENT_COMPAT, 'UTF-8') .'</em>';
        }
    }
    

    Reference: PHP MySQL display multiple rows grouped by common fields

    On the other hand, if it's used to track prescriptions, then have a separate table along the lines of id | drug_id | dosage_amount, along with any other information you would require.

    If you went with the second option, you would use an INNER JOIN to get the required data:

    SELECT drugs.name, prescriptions.dosage_amount
    FROM drugs
    INNER JOIN prescriptions
    ON drugs.id=prescriptions.drug_id
    

    Reference: http://www.w3schools.com/sql/sql_join_inner.asp

    From there, you can iterate through it the same way you were doing it already:

    while ($row = mysql_fetch_array($result)) {
        echo "Drug Name: {$row[0]} Drug Weight: {$row[1]}"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么