How do you do sql queries that use relationships?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kzar
Forum Newbie
Posts: 16
Joined: Thu Nov 25, 2004 3:03 pm

How do you do sql queries that use relationships?

Post 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]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
kzar
Forum Newbie
Posts: 16
Joined: Thu Nov 25, 2004 3:03 pm

Post by kzar »

yea searching about sql joins showed me what i wanted thanks. (I didnt know they where called joins before)

thanks
Post Reply