Having some tables where I store information about a patient and type of tests (there are 3 types of tests, those I store in other table) patient were exposed I got:
CREATE TABLE IF NOT EXISTS patients (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Name VARCHAR(50) NOT NULL,
Tel VARCHAR(35) NOT NULL,
Email VARCHAR(35) NOT NULL
);
CREATE TABLE IF NOT EXISTS tests (
id_test INTEGER PRIMARY KEY AUTOINCREMENT,
id_patient INTEGER,
id_type_test INTEGER,
id_graph INTEGER,
id_graph2 INTEGER,
date_test VARCHAR(10),
FOREIGN KEY (id_patient) REFERENCES patients(id_patient) ON DELETE CASCADE,
FOREIGN KEY (id_type_test) REFERENCES type_tests(id_type_test) );
CREATE TABLE IF NOT EXISTS type_tests (
id_type_test INTEGER PRIMARY KEY AUTOINCREMENT,
name_test VARCHAR(6) NOT NULL
);
INSERT INTO type_tests values (1,'Audi');
INSERT INTO type_tests values (2,'Log');
INSERT INTO type_tests values (3,'na');
INSERT INTO patients values (1,'John','5653346','john@foe.com');
INSERT INTO patients values (2,'Karla','234346','karla@foe.com');
INSERT INTO patients values (3,'Chad','6667653346','chad@foe.com');
INSERT INTO tests values (1,1,1,50,160,'02/02/2010');
INSERT INTO tests values (2,1,3,55,165,'12/05/2010');
INSERT INTO tests values (3,1,3,65,170,'03/12/2010');
I would like to retrieve all patient information of all patients plus their studies ordered by most recent date like:
ID Name Tel Email Date Test graph graph2
To solve that first I tried getting all studies of one patient like:
SELECT te.name_test AS Test,
e.date_test AS Date,
e.id_graph AS id_graph,
e.id_graph2 AS id_graph2
FROM tests e
INNER JOIN type_tests te
ON e.id_type_test = te.id_type_test
WHERE e.id_patient=1
ORDER BY
e.date_test DESC;
I got:
Test Date id_graph id_graph2
na 12/05/2010 55 165
na 03/12/2010 65 170
Audi 02/02/2010 50 160
How can I get all patient information of all patients?
Here is a sqlfiddle
desired output would be
ID Name Tel Email Test Date id_graph id_graph2
1 John 5653346 john@foe.com na 12/05/2010 55 165
1 John 5653346 john@foe.com na 03/12/2010 65 170
1 John 5653346 john@foe.com Audi 02/02/2010 50 160
If there were more patients I would like to show all patients...