I'm currently doing a patient record system for a dental clinic. I need to display all treatments done to the patient, but I have two tables for treatment. Is there a way to combine columns from the two different tables?
Both of the tables have appointmentid, datecreated, procedure. I tried using coalesce function, but when the appointmentid is present in both tables, only the first one displays.
$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>
';
}
}
Expected result:
1|2004-12-05|Cleaning (from treatment1)
1|2004-12-05|Tooth Extraction (from treatment2)
Try this query: 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
It gives the following output:
id datecreated procedures amount
1 2004-12-05T00:00:00Z Cleaning 100
1 2004-12-05T00:00:00Z Tooth Extraction 150
Alternatively, the following query gives the same results:
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