Page 1 of 1

How do you do sql queries that use relationships?

Posted: Mon Dec 27, 2004 6:45 am
by kzar
feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi im writting a script for my coursework and I have run into a spot of trouble. Im not sure how to do linked queries, my tables are set up with primary keys and I have made sure there is always a field to link things together.

Heres a little example of one thing I need to do to get the idea across. Here are some tables.

Code: Select all

CREATE TABLE `tt_lessons` (
`LessonID` INT NOT NULL AUTO_INCREMENT ,
`TeacherID` INT NOT NULL ,
`ClassRoomID` INT NOT NULL ,
`SubjectID` INT NOT NULL ,
`BlockID` INT NOT NULL ,
PRIMARY KEY ( `LessonID` )
);

CREATE TABLE `tt_teachers` (
`TeacherID` INT NOT NULL AUTO_INCREMENT ,
`TeacherName` TEXT NOT NULL ,
PRIMARY KEY ( `TeacherID` )
);
Now say if I want to do a query which would check for lessons with certain requirements and then output the teachername is it possible? Would i have to get the teacherid and then write another query which uses the id to get the teacher name?

If it helps Im using php but its more of a general sql question.

Thanks for the help, david


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Dec 27, 2004 8:03 am
by timvw
just search the web for a tutorial on basic sql or joins... you'll find probably more than enough good stuff that explains you everything you wanted to know and more...

Posted: Mon Dec 27, 2004 9:18 am
by kzar
yea searching about sql joins showed me what i wanted thanks. (I didnt know they where called joins before)

thanks