It’s hard to explain but I have this table, incurredcharges where it has 3 columns (no, patient_no, procedure_no) Ignore the “no” column. The other table that I need is the charges, where the names of the procedures are stored. So I want to output the procedure_no with its corresponding procedure name.
The problem is, “charges” isnt the only table I need. Cause I was so stupid that I separated the procedure names of the ER charge, ultrasound, and the confinement. Therefore producing THREE tables that have the same structures.
SELECT incurredcharges.procedure_no, incurredcharges.patient_no, charges.procedure
FROM incurredcharges
INNER JOIN charges
ON incurredcharges.procedure_no=charges.procedure_no
WHERE incurredcharges.patient_no=12;
That statement works but it only shows the procedure name FROM the ER charge only. Ultrasound and confinement procedure names will not be shown!
For the immediate need, you can
UNION ALLthe 3 tables together as a derived table toJOINagainst.But for the future:
The correct long-term fix, however, is to use a similar
UNION ALLstructure as used in the subquery to insert all these records back into a unified table, with a new column that identifies their source.Note:
procedureis a MySQL reserved keyword that must be quoted with backticks, so I just quoted everything.