dinsdale Posted April 7, 2009 Share Posted April 7, 2009 hello all, one thing ive had alot of trouble with is associating tables with other tables, example I make 3 tables right, create table Scientists{ cod varchar(, name varchar(255) }; create table associated{ scientist varchar(, project varchar(4) }; create table project{ ID varchar(4), namepro varchar(50), hours integer }; Not sure how to put the keys now how do i do a query say, Show all the scientists (thier name), what project they are doing and the hours of the project thanks Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/ Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 First we need a way of linking all the tables with each other. In your table "Scientists" I can't see a numerical primary key, something like this would be handy: id INT UNSIGNED PRIMARY_KEY AUTO_INCREMENT Then in "associated" we need to somehow link this to Scientists so we can add a field called scientistid in there scientistid INT UNSIGNED Then with the "project" table we need to somehow link it to the scientist - again, add the scientistid as used above. Now we need to decide which way to join them in a query - will there always be data in associated and project for a scientist or is the data optional? Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803397 Share on other sites More sharing options...
dinsdale Posted April 7, 2009 Author Share Posted April 7, 2009 Yes associated needs data Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803405 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 I've just created these tables on my server so I can present you with some actual code... CREATE TABLE `scientists` ( `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 255 ) NOT NULL ) ENGINE = InnoDB CREATE TABLE `project` ( `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `scientistid` SMALLINT UNSIGNED NOT NULL DEFAULT '0', `projectname` VARCHAR( 255 ) NOT NULL , `hours` SMALLINT UNSIGNED NOT NULL DEFAULT '0' ) ENGINE = InnoDB Let's say you've got a lot of scientists listed and not all of them have a project assigned to them: SELECT s.*,p.* FROM scientists AS s LEFT JOIN project AS p ON s.id=p.scientistid WHERE s.id=$id That will pull all information from the database (both tables) where a scientist exists with the id of $id and if they are associated to a project that information will be shown as well. I don't think there's any need to link with a third table. Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803409 Share on other sites More sharing options...
dinsdale Posted April 7, 2009 Author Share Posted April 7, 2009 Thanks for your effort, "I don't think there's any need to link with a third table." I always tought that aswell but the problem is.. thats how my teacher wants it done. the tables i wrote here, come from the exercise sheet. and thus, creates alot of confusion for me hehe Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803417 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 Example data for scientists: id,name 1, Adam 2, Brian 3, Charlie 4, Dave 5, Edward Example data for project: id, scientistid, project, hours 1, 1, Splitting the Atom, 5 2, 1, Making Gold, 10 3, 2, Invisility, 4 Let's pull out Adam's work: SELECT s.*,p.* FROM scientists AS s LEFT JOIN project AS p ON s.id=p.scientistid WHERE s.id=1 Will return: id name id scientistid projectname hours 1 Adam 1 1 Splitting the Atom 5 1 Adam 2 1 Making Gold 10 Let's pull out Charlie's work: SELECT s.*,p.* FROM scientists AS s LEFT JOIN project AS p ON s.id=p.scientistid WHERE s.id=3 Will return: id name id scientistid projectname hours 3 Charlie NULL NULL NULL NULL Because Charlie doesn't have any projects in the database all we can get is his unique ID number and his name - the other fields are returned as NULL (aka empty) Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803424 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 Thanks for your effort, "I don't think there's any need to link with a third table." I always tought that aswell but the problem is.. thats how my teacher wants it done. the tables i wrote here, come from the exercise sheet. and thus, creates alot of confusion for me hehe We need to make that third table a link table then linking the two. Can you explain what "cod" is please? Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803427 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 I can see project in "associated" is used to link to "project" table. What field in "Scientists" links to which field in "associated"? Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803434 Share on other sites More sharing options...
dinsdale Posted April 7, 2009 Author Share Posted April 7, 2009 Well i guess scientist.cod with associated.scientist since they both have the same size Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803440 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 SELECT s.*,p.* FROM scientists AS s,associated AS a,project AS p WHERE s.cod=a.scientist AND a.project=p.id AND s.name='Adam' I think that's what you're looking for. EDIT: Added WHERE clause to allow us to pick out a scientist by name. Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803445 Share on other sites More sharing options...
dinsdale Posted April 7, 2009 Author Share Posted April 7, 2009 yeah thats what ive been looking for.. thanks a ton mate Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803448 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 You're wekcome - hope the "LEFT JOIN tutorial/guide" there helps Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803454 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 btw, the type of join we've finally ended up with is an OUTER JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803458 Share on other sites More sharing options...
dinsdale Posted April 7, 2009 Author Share Posted April 7, 2009 Ah thanks, ill have a look at a few tuts Quote Link to comment https://forums.phpfreaks.com/topic/152974-joining-tables-with-other-tables/#findComment-803481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.