将来自两个不同表的两组数据合并到一列中

时间:2019-04-22 03:14:21

标签: php sql

我目前正在为牙科诊所做病人记录系统。我需要显示对患者进行的所有治疗,但是我有两个治疗表。有没有一种方法可以合并来自两个不同表的列?

这两个表都有约会ID,创建日期,过程。我尝试使用合并功能,但是当两个表中都存在约会ID时,仅显示第一个。

$sql3 = "SELECT * FROM appointment WHERE patient_number = '".$_GET['patient_number']."'";
$result3 = mysqli_query($conn, $sql3);
    while($row3 = mysqli_fetch_array($result3)) { 

       $sql1 = "SELECT appointment.id, coalesce(alltreatment.datecreated, toothtreatment.datecreated) AS col0 , 
             coalesce(alltreatment.appointmentid, toothtreatment.appointmentid) AS col1, 
             coalesce(alltreatment.procedures, toothtreatment.procedures) AS col3, 
             coalesce(alltreatment.amount, toothtreatment.amount) AS col4
            FROM appointment LEFT JOIN alltreatment ON appointment.id = alltreatment.appointmentid                                               
            LEFT JOIN toothtreatment ON appointment.id = toothtreatment.appointmentid                                            
           WHERE appointment.id = '".$row3['id']."' ORDER BY col1 DESC";

            $result1 = mysqli_query($conn, $sql1);

            while($row1 = mysqli_fetch_array($result1)) {  
                          echo '
                       <tr>  

                            <td>'.$row1['col1'].'</td>
                            <td>'.$row1['col0'].'</td>
                            <td>'.$row1['col3'].'</td>

                       </tr>  
            ';
            }
    }

预期结果:

1|2004-12-05|Cleaning (from treatment1)
1|2004-12-05|Tooth Extraction (from treatment2)

1 个答案:

答案 0 :(得分:0)

尝试以下查询:http://www.sqlfiddle.com/#!18/4dd42/5

create table appointment (id int)
create table alltreatment (id int, appointmentid int, procedures varchar(50), amount int, datecreated datetime)
create table toothtreatment (id int, appointmentid int, procedures varchar(50), amount int, datecreated datetime)

insert into appointment (id) values (1)
insert into alltreatment (id, appointmentid, procedures, amount, datecreated) values (10, 1, 'Cleaning', 100, '20041205')
insert into toothtreatment (id, appointmentid, procedures, amount, datecreated) values (20, 1, 'Tooth Extraction', 150, '20041205')

SELECT
    appointment.id, 
    alltreatment.datecreated,
    alltreatment.procedures,
    alltreatment.amount
FROM appointment
INNER JOIN alltreatment ON appointment.id = alltreatment.appointmentid 
WHERE appointment.id = 1
UNION ALL
SELECT
    appointment.id, 
    toothtreatment.datecreated,
    toothtreatment.procedures,
    toothtreatment.amount
FROM appointment
INNER JOIN toothtreatment ON appointment.id = toothtreatment.appointmentid 
WHERE appointment.id = 1
ORDER BY appointment.id DESC

它提供以下输出:

id  datecreated             procedures          amount
1   2004-12-05T00:00:00Z    Cleaning            100
1   2004-12-05T00:00:00Z    Tooth Extraction    150

或者,以下查询给出的结果相同:

SELECT
    appointment.id, 
    t.datecreated,
    t.procedures,
    t.amount
FROM (
    SELECT
        appointmentid, 
        datecreated,
        procedures,
        amount
    FROM alltreatment
    UNION ALL
    SELECT
        appointmentid, 
        datecreated,
        procedures,
        amount
    FROM toothtreatment 
) t
INNER JOIN appointment ON appointment.id = t.appointmentid 
WHERE appointment.id = 1
ORDER BY appointment.id DESC