Jump to content

[SOLVED] simple join


Stooney

Recommended Posts

Not too familiar with table joins yet.  I'll keep it short.

 

I have a table called events_signup.  I pull all of the event_id's from this table where user_id=the user.  Easy, simple query.

 

Now I need to query the events table to pull the event information for each of the event id's I just got.  I'm not sure how to go about this without a separate query for each event id.  Not even sure what keywords to google.  Any help is appreciated.

Link to comment
Share on other sites

SELECT events_signup.EVENT_ID, events.EVENT_NAME
FROM events_signup LEFT JOIN events ON events.EVENT_ID = events_signup.EVENT_ID
WHERE USER_ID = 1

 

This assumes that the events table has a column called EVENT_NAME, so replace the columns with the one you need.  This will include all events, even if they don't have an event name because it is a LEFT JOIN.

 

You could also do this instead, which will only include all events that have an event name.

 

SELECT events_signup.EVENT_ID, events.EVENT_NAME
FROM events_signup, events
events.EVENT_ID = events_signup.EVENT_ID
AND USER_ID = 1

Link to comment
Share on other sites

That looks close to what I'm looking for.  I will explain better this time. 

 

Here is the 'long' way of doing what I need.  This is with all the real variable/table names/fields.  This will return an array of resource, but I need it all in a single resource.

 

<?php
//Grabs all the events the user is signed up for
$event_temp=Dbc::query("SELECT event_id FROM ".DB_PREFIX."_events_signup WHERE user_id='$user_id'");

//Grabs all the info for each event the user is signed up for
while($row=Dbc::fetch($event_temp)){
$events[]=Dbc::query("SELECT * FROM ".DB_PREFIX."_events WHERE date>NOW() AND event_id='$row[event_id]'");
}
?>

Link to comment
Share on other sites

TY sir. 

 

Final Solution:

Dbc::query("SELECT ".DB_PREFIX."_events_signup.event_id, ".DB_PREFIX."_events.* FROM ".DB_PREFIX."_events_signup, ".DB_PREFIX."_events WHERE ".DB_PREFIX."_events_signup.event_id = ".DB_PREFIX."_events.event_id  AND user_id='$user_id' AND ".DB_PREFIX."_events.date > NOW()");

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.