2013-06-01 64 views
0

我在我的mysql數據庫中有3個表:student, progress and subject。當我嘗試選擇數據:如何鏈接表mysql

SELECT progress.id, progress.mark, subject.pname FROM progress, subject WHERE progress.id_student = 1; 

下表我得到:

id mark pname 
1 5  Math 
1 5  Physics 

表進步我只有一個條目:

id mark id_student id_subject 
1 5  1   1 

我怎樣才能通過student_id得到學生的進步?

CREATE DATABASE students; 
USE students; 
CREATE TABLE student (
id int NOT NULL AUTO_INCREMENT, 
name varchar(100) NOT NULL, 
address varchar(60) NOT NULL, 
byear int NOT NULL, 
eyear int NOT NULL, 
PRIMARY KEY (id) 
); 
CREATE TABLE subject (
id int NOT NULL AUTO_INCREMENT, 
pname varchar(20) NOT NULL, 
PRIMARY KEY (id) 
); 
CREATE TABLE progress (
id int NOT NULL AUTO_INCREMENT, 
mark int NOT NULL, 
id_student int NOT NULL, 
id_subject int NOT NULL, 
PRIMARY KEY (id), 
FOREIGN KEY (id) REFERENCES student (id), 
FOREIGN KEY (id) REFERENCES subject (id) 
); 
+0

你已經得到output.So什麼問題 – PSR

回答

1
SELECT progress.id, progress.mark, subject.pname 
FROM progress LEFT OUTER JOIN subject ON (progress.id_subject= subject.id) 
WHERE progress.id_student = 1; 
+0

非常感謝你,你幫了我很多 – stark

1

嘗試

SELECT progress.id, progress.mark, subject.pname 
FROM progress, subject 
WHERE progress.id_student = 1 AND progress.id_subject = subject.id;