0

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

4

1 回答 1

0

对于您在问题中显示的特定值,可以使用此查询。

SELECT q2.primaryid, q_1, q_2, q_4, q_5 FROM questions_101_200 q2
CROSS JOIN questions_1_100 

但是你的数据和结构对我来说没有多大意义。如果您解释更多有关您要做什么的信息,我可以给您一个更有意义的答案。

于 2013-05-03T13:24:56.783 回答