I have the following tables and data:
Tables
-----------
CREATE TABLE primarys
( primaryid BIGINT(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (primaryid));
CREATE TABLE patients
( primaryid BIGINT(20) NOT NULL,
patientid BIGINT(20),
patientName VARCHAR(50),
PRIMARY KEY (primaryid));
CREATE TABLE patientvisit
( primaryid BIGINT(20) NOT NULL,
visitid BIGINT(20),
visitdate DATE,
PRIMARY KEY (primaryid));
Data
---------
INSERT INTO primarys values (1);
INSERT INTO primarys values (7286);
INSERT INTO primarys values (7287);
INSERT INTO patients VALUES (1,'1','John');
INSERT INTO patients VALUES (7286,'1', '');
INSERT INTO patients VALUES (7287,'1', '');
INSERT INTO patientvisit VALUES (7286,'1','1997-12-18');
INSERT INTO patientvisit VALUES (7287,'2','1998-02-25');
I need to write a query that outputs the data as follows:
primaryid | patientid | patientname | visit | visitdate
------------------------------------------------
7286 | 1 | John | 1 | 1997-12-18
7287 | 1 | John | 2 | 1998-02-25
I can figure out how to do this with outer joins and sub queries which work fine but when I start adding a large data set mysql performance begins to significantly decrease.
I would be very grateful if anyone could suggest the most optimised way to query these data and get the desired output.
Thanks