Jump to content

[SOLVED] Joining Tables


bruckerrlb

Recommended Posts

I seem to be having a problem joining tables for a report I'm working on and wanted to see if I could get some feedback. I'm trying to join tables that aren't directly relational, I have the following tables:

users

tickets

customfields

customfieldvalues

The only way I can establish a relationship between users and customfields and their values is through the following method

users.userid = tickets.userid

tickets.ticketid = customfieldvalues.typeid

customfieldvalues.customfieldid = customfields.customfieldid

 

I've been reading about joins but can't seem to figure out which one would be best, can anyone give some feedback on the best way to join these tables?

 

Link to comment
Share on other sites

Thanks for the fast response, first I'll show the tables

Users

userid

usergroupid

fullname

phone

userpassword

userpasswordtxt

dateline

lastvisit

lastactivity enabled

loginapi_moduleid

loginapi_userid

languageid

timezoneoffset

enabledst

useremailcount

allowemail

slaplanid

slaexpiry

ismanager

Tickets

ticketid

ticketmaskid

departmentid

ticketstatusid

priorityid

emailqueueid

userid staffid

ownerstaffid

assignstatus

fullname

email

lastreplier

replyto

subject

dateline

lastactivity

laststaffreplytime

slaplanid

ticketslaplanid

duetime

totalreplies

ipaddress

flagtype

hasnotes

hasattachments

isemailed

edited

editedbystaffid

editeddateline

creator

charset

transferencoding

timeworked

dateicon

lastpostid

firstpostid

tgroupid

messageid

escalationruleid

hasdraft

hasbilling

isphonecall

isescalated

phoneno

autoclosetimeline

islabeled

lastuserreplytime

escalatedtime

followupcount

Custom Fields

customfieldid

customfieldgroupid

title

fieldtype

fieldname

defaultvalue

isrequired

usereditable

staffeditable

regexpvalidate

displayorder description

Custom Field Values

customfieldvalueid

customfieldid

typeid

fieldvalue

isserialized

 

 

I"m trying to output all users where customfields.customfieldid the value for 51 is not null, and then I ultimately want to spit out the following:

username --> users.fullname

usergroup --> users.usergroupid

Phase --> customfieldvalues.customfieldid = 50

Number --> customfieldvalues.customfieldid = 51

Report # --> customfieldvalues.customfieldid = 52

Address --> customfieldvalues.customfieldid = 22

city --> customfieldvalues.customfieldid = 5

state --> customfieldvalues.customfieldid =6

zip --> customfieldvalues.customfieldid = 30

phone --> customfieldvalues.customfieldid = 7

status --> customfieldvalues.customfieldid = 34

 

That's what my report is going to consist of, but I'm having difficulties writing this sql statement.

 

 

Link to comment
Share on other sites

After doing some more research, found out that the tickets table isn't necessary, because users.userid = customfieldvalues.typeid. So now I"m trying to do a join specifically from the users table to customfieldvalues and customfields and I have something along these lines

$query_customfield = mysql_query("SELECT `swusers` LEFT JOIN `swcustomfieldvalues` as cfvalues ON swusers.userid = cfvalues.typeid") or die(mysql_error());

 

This is my query as it stands now, but it's not shooting out anything. What I'm trying to do is figure out a way I can test to see if a user has a customfieldvalue.customfieldid that is equal to 51, then I want to be able to bring back users.fullname, as well as fieldname.customfields and fieldvalue.customfieldvalues for that user (users.fullname) Any recommendations?

 

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.