MySQL为每条记录检索多条记录

时间:2015-01-21 15:23:04

标签: php mysql sql

我有两张桌子,病人和保险。每个患者可以有几个保险,所以我需要在不同的行中检索每个患者和他们的每个保险,这样我就可以将它们放在JSON阵列中。例如:

{
    patient: John,
    address: apple,
    race: white
    insurances: // array of insurances for this patient
}

我是否必须使用PHP检索每位患者,然后使用他们的ID来获取每份保险记录?对于属于每个患者记录的每组行,是否有更快的方法在MySQL中检索数组?谢谢大家。

患者

-----+---------+------+------------
name | address | race | insuranceID
-----+---------+------+------------
john | apple   | white| 1
-----|---------|------+------------
mary | jane    | asian| 2
-----|---------|------+------------
sally| kay     | black| 3

保险

----+------+---------+------
 id | name | address | type
----+------+---------+------
  1 | edna | 112 n   | P
----+------|---------|------
  2 | maid | 45 st   | I
----+------|---------|------
  3 | mcare| 1235    | P

注意 我正在寻找实施的想法,而不是修复我已经拥有的东西,我什么都没有......

3 个答案:

答案 0 :(得分:2)

您的目标数据结构似乎是一个php数组,我们称之为patients,其中每个元素本身就是一个描述患者的数组。

抛开MySQL,你可以在php中这样做:

$patients = array();
/* for each patient */
  $patient = array();
  $patient[name]  = 'john';
  $patient[address] = '123 apple';
  $patient[race] = 'caucasian';
  /* for each payer for that patient */
    $insurance = array();
    $insurance[name] = 'edna';
    $insurance[type] = 'P';
    $patient[insurances[]] = $insurance;
  $patients[] = $patient;
/* now you can json encode the result */
$jpatient = json_encode($patients);

因此,此处的诀窍是让您的MySQL结果集符合此代码中的for each patientfor each payer

你可以通过发出一个说

的查询来做到这一点
SELECT p.id, p.name, p.address, p.race,
       i.id payerid, i.name payername, i.type payertype
  FROM patients p
  JOIN insurances i ON p.id = i.patient_id
 ORDER BY p.id, i.id

这将为每个付款人提供一行结果集。如果患者有多个付款人,则将重复患者的数据(连续行)。因此,您需要在PHP代码中检测患者ID何时发生变化。

看起来像这样。 (我道歉,我没时间调试这个。)

$patients = array();
$patient = false;
$previousid = -1;
while ( $row = $resultset->fetch_assoc() ) {
  if ( $row[id] != $previousid ) {
    /* this is a new patient */
    if ( !empty($patient) ) {
       /* we were already accumulating data for a patient */
       $patients[] = $patient;
       $patient = false;
       $previousid = $row[id];
    }
  }
  if ( empty ($patient) ) {
    /* we need to start a new patient data structure */
    $patient = array();
    $patient[name] = row[name];
    $patient[address] = row[address];
    $patient[race] = row[race];
  }
  $insurance = array();
  $insurance[name] = $row[payername];
  $insurance[type] = $row[payertype];
  $patient[insurances[]] = $insurance;
} /* end while fetch_assoc() */
if ( !empty($patient) ) {
  /* output last patient if any */
  $patients[] = $patient;
}

答案 1 :(得分:0)

如果患者和保险之间存在多对多的关系,我会再添加一个名为Patient_Insurance的表,并将其作为链接表:

---+------+ Pid| Iid | ----+------+ 1 | 1 | ----+------| 1 | 2 | ----+------| 2 | 1 |

然后我想象的最快的方法就是通过从患者中选择所有来填充您的Patient阵列,然后通过在Patient_Insurance中查找用户ID并加入Insurances数据来遍历该阵列并填充保险。

您只需一次查询就无法获得所需的结果。

答案 2 :(得分:0)

这是一个有效的SQLFiddle of what you want。请注意,这只是一般性的近似,我没有包括所有字段或设置主键,FK等。

"很多" table(保险)需要参考" one"表(患者)是PatientID

create table Patient (id int, name varchar(20));
create table Insurance (id int, address varchar(100), PatientID int);

然后你可以查询:

SELECT p.id, p.name,
       i.id insuranceID, i.address
  FROM Patient p
  JOIN Insurance i ON p.id = i.PatientID
 ORDER BY p.id, i.id

得到:

ID  NAME    INSURANCEID     ADDRESS
1   bob     1               1 main st
1   bob     2               1 left st
1   bob     3               1 right st
3   joe     4               joe lives here
3   joe     5               joe other house
相关问题