有一些表格,我存储有关患者的信息和测试类型(有3种类型的测试,我存储在其他表中的那些)患者暴露了我得到了:
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');
我想检索所有患者的所有患者信息以及最近一次订购的研究,如:
ID Name Tel Email Date Test graph graph2
为了解决这个问题,我首先尝试了对一位患者的所有研究,如:
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;
我得到了:
Test Date id_graph id_graph2
na 12/05/2010 55 165
na 03/12/2010 65 170
Audi 02/02/2010 50 160
如何获得所有患者的所有患者信息?
期望的输出将是
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
如果有更多患者,我想向所有患者展示......
答案 0 :(得分:1)
只需加入patients
表格:
SELECT p.ID,
p.Name,
p.Tel,
p.Email,
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
INNER JOIN patients p
ON p.ID = e.id_patient
WHERE e.id_patient=1
ORDER BY
e.date_test DESC;