Извличане от една таблица използвайки друга

wizzyto

Registered
Въпроса ми е следния:

При търсене: Търся номер (идентификационен) от таблица names (колона id) и след като го взема да извлека от друга таблица всичките му оценки (примерно взимам ID-то и после по същото ID в друга таблица намирам оценките) ?
 
След като вече си извлякал това ID, правиш друга заявка изглеждаща горе долу по следния начин:

Код:
$q=mysql_query("select * from ocenki where id='$id'")
, като $id ти е извлеченото ID.
 
Код:
create table student (
   id int(10) unsigned not null primary key auto_increment,
   name varchar(100) not null
);

insert into student values (NULL, 'Ivan Ivanov'), (NULL, 'Peter Petrov');


create table major (
   id int(10) unsigned not null primary key auto_increment,
   name varchar(100) not null
);

insert into major values (NULL, 'Mathematics'), (NULL, 'Biology');

create table mark (
   id int(10) unsigned not null primary key auto_increment,
   student_id int(10) unsigned not null,
   major_id int(10) unsigned not null,
   score decimal(3,2) not null
);

insert into mark values (NULL, 1, 1, '5.40'), (NULL, 1, 2, '4.30'), (NULL, 2, 1, '6.00'), (NULL, 2, 2, '4.70');

select student.name as 'Student', major.name as 'Major', mark.score as 'Score' from student left join mark on student.id = mark.student_id left join major on mark.major_id = major.id;

+---------------+---------------+-------+
| Student        | Major              | Score |
+---------------+---------------+-------+
| Ivan Ivanov  | Mathematics    |  5.40 |
| Ivan Ivanov  | Biology            |  4.30 |
| Peter Petrov  | Mathematics    |  6.00 |
| Peter Petrov  | Biology            |  4.70 |
+---------------+-------------+-------+

Причина за редакцията: една ненужна звезда в последната заявка
 
select student.name as 'Student', major.name as 'Major', mark.score as 'Score' * from student left join mark on student.id = mark.student_id left join major on mark.major_id = major.id;

Това ми трябваше. Благодаря много!
 

Back
Горе