【问题标题】:Returning multiple values of a column返回列的多个值
【发布时间】:2013-04-02 16:36:27
【问题描述】:

我在我的数据库中创建了一个诊所表,其中包含诊所的详细信息,例如。 ClinicID(主键)、姓名、地址、电子邮件等。我有另一个名为 Doctor 的表,它根据 ClinicID 列出诊所中医生的姓名。问题是,在一个诊所中,每个诊所可能有超过 1 名医生。如何在 html 表格中显示诊所的详细信息,该表格将列出所有详细信息,包括诊所可用的所有医生?

临床表

clinicID
name
address
email

医生桌

clinicID
nameOfDoctor

【问题讨论】:

  • 使用每个诊所“行”内的医生“列表”
  • 要么运行两个查询,要么使用 group_concat() 之类的东西。
  • 这是 UI 问题、DB 问题还是某种组合?

标签: php sql


【解决方案1】:
select nameOfdoctor from Doctor where ClinicId=1

将向您显示诊所 1 的医生名单

【讨论】:

    【解决方案2】:

    您必须进行两次调用来整理每个查询中的信息:

    $raw_clinics = //sql query to get clinic data
    $doctors = //sqlquery to get doctors data
    $clinics = array();
    
    foreach($raw_clinics as $c) {
        $clinics[$c['clinicID']] = $c;
    }
    
    foreach($doctors as $d) {
        if (!isset($clinic[$d['clinicID']]['doctors'])) $clinic[$d['clinicID']]['doctors'] = array();
        $clinic[$d['clinicID']]['doctors'][] = $d;
    }
    

    【讨论】:

      【解决方案3】:

      你需要两个迭代循环来得到这个(假设你想要一个诊所列表):

      $clinicResult = $mysqli->query("SELECT * FROM Clinic");
      while ($clinicData = $clinicResult->fetch_assoc()) {
          //Insert your HTML statements regarding clinic details here
          $doctorResult = $mysqli->query("SELECT * FROM Doctor WHERE clinicID = ".$clinicData['clinicID']);
      
          while ($doctorData = $doctorResult->fetch_assoc() {
              //Insert your HTML statements regarding doctors here
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-29
        • 2011-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        • 1970-01-01
        • 2021-11-30
        相关资源
        最近更新 更多