Jump to content

MySQL Join


guitarist809

Recommended Posts

OK, here are the tables that I currently have:
[activities] - Contains all sorts of activities
+--------------+
activity_id - e.x., 1
category - climb
subcategory - climbing
summaryrpt - yes
abrev - clmb

[locations] - all sorts of locations in the world
+--------------+
location_id    //1
location  //Mount Everest
city  //N/A
state
country
notes //high mountian

[trips] - all tables link here, after here
+-------------+
trip_id //1
location_id //1 (links to table location, location would be Mount Everest)
trip_date // day trip was on

[trips_activities] - activities completed by users in database
+-------------+
activity_id    //id for index
trip_id //links to trip table
act_id  //links to activities table
user_id    //links to users (table specified below)

[trips_comments] - comments left by each user on each trip
+-------------+
comment_id //index
trip_id  //links to trip
user_id  //links to users (table will be specified blow)
comment //the comment by that user

[trips_participants]
+----------------+
participant_id  //index
trip_id  //links to trip
user_id  //links to the users table which will be specified next

[users]
+-------------+
user_id //index
username  //username for login
name //users name
password //users password
config_sys //permissions
config_trip  //permissions

Wow, there's like 4 more tables, but i think i can figure it out if i see an example.

This is what im trying to do:

Grab the trips in order by date ASC, then get all the users who went on the trip (table: trips_participants), then grab the comments left by those users(table: trips_comments), then grab all activities for each user (table trips_activities), then get all the names of the activities (table: activities), then get the location of the trip (table: trips), then get the name of the location (table: locations).

Is this even possible with a join?

Thanks,
Matt :D
Link to comment
Share on other sites

first of all, I think the database design you had is well structured and binded.  Good design.
You do not need more than 3 joins level to get what you wanted.

Here is 1 example of using join, there are many type of join: left join, right join and join.  So, I'm not sure about restriction of each table.



This is one example:
This query return a user's name, user's comment, trip activities and trip id:

[code]
"select users.username as p_username, trips_comments.comment as p_comment, trips_activities.activity_id as p_activity_id,
trips_comments.trip_id as p_trip_id from users join trip_comments on users.user_id = trips_comments.user_id join trips_activities
on users.user_id = trip_activities.user_id;
[/code]
I wrote this merely by looking at table structure.  If it doesn't work, post both query and error.
if you need a simple example on a specific thing, let me know also.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.