Razor42 Posted August 17, 2012 Posted August 17, 2012 Hello there would be greatful if someone could help me with a problem I am having. Basically I want to create a query which selects from three tables in my database, I know this query uses the JOIN and I have a basic knowladge of what it should look like but I am not 100% sure how to do it. I know it should be something along the lines of: $dd = $db->query("SELECT * FROM `table1` JOIN `table2` ON `table1.id` = `table2.id` JOIN `table3` ON `table2.id` = `table3.id` WHERE `table1.id` = 1"); I think.... Table 1 should be- `dogs` Table 2 should be- `dogs_stats` Table 3 should be- `dogs_bgc` But I am unsure what to put in `table1.id`,`table2.id` and `table3.id` If someone could point me in the right direction that would be great. Quote
newttster Posted August 17, 2012 Posted August 17, 2012 You would need to have an identifier that is the same across all tables. For example ... users.userid to userstats.userid to inventory.inv_userid. That kind of thing. Quote
Seker Posted August 17, 2012 Posted August 17, 2012 You would need to have an identifier that is the same across all tables. For example ... users.userid to userstats.userid to inventory.inv_userid. That kind of thing. To clarify that just a little bit, the column names don't have to be the same. They just have to hold the same values. Quote
newttster Posted August 17, 2012 Posted August 17, 2012 To clarify that just a little bit, the column names don't have to be the same. They just have to hold the same values. My apologies for not making that clearer ... but yes ... they have to be the same type and hold the same value. Quote
Razor42 Posted August 17, 2012 Author Posted August 17, 2012 So I need to create a common value in each table? Something like `dog_id`? Which would make the query: $dd = $db->query("SELECT * FROM `dogs` JOIN `dogs_stats` ON `dogs.dog_id` = `dogs_stats.dog_id` JOIN `dogs_bgc` ON `` = `dogs_bgc.dog_id` WHERE `dogs.dog_id` = 1"); Would that be correct? Quote
Spudinski Posted August 19, 2012 Posted August 19, 2012 Joins, joins everywhere. LEFT, RIGHT and CROSS. ...and natural, inner, outer.. Quote
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.