php - Need help about joining tables -


i have mysql database , i'm trying create web interface manage tickets, right i'm trying list tickets so: [title][name of person created ticket][priority][date created][peoples in charge of ticket]

so have table named tickets title, id of person created ticket, priority, date.

i have table named users can find first , last name , other informations id (you can link 2 tables id)

i have table named tickets_users can find id of peoples in charge of tickets

my problem don't know how link of in 1 request, simple if 1 people in charge of ticket there can multiple persons, tried queries tickets titles etc in double when there more 1 people in charge of ticket.

thanks in advance edit example of tables:

tickets:    -id = 523 | title = internet explorer | priority = 3 | date = 2013-10-10 11:20:51 users:     -id = 25 | firstname = john | lastname = light    -id = 35 | firstname = dwight | lastname = night    -id = 53 | firstname = maria | lastname = sun tickets_users :    -ticketid = 523 | userid = 25 | type = 1    -ticketid = 523 | userid = 35 | type = 2    -ticketid = 523 | userid = 53 | type = 2 

and i'd able request display:

[help internet explorer][john light][3][2013-10-10 11:20:51][maria sun - dwight night] 

in 1 line (per ticket) , tickets in db

you can use group_concat aggregate function group names of linked persons single field in result. since don't have exact table structure, i've made names of fields , tables.

select   t.title,   group_concat(     case when tu.type = 1        concat(u.firstname, ' ', u.lastname)     end) creator,   t.priority,   t.date,   group_concat(     case when tu.type = 2        concat(u.firstname, ' ', u.lastname)     end separator ' - ') users   tickets t   inner join tickets_users tu on tu.ticketid = t.id   inner join users u on u.id = tu.userid group   t.id; 

if there indeed 1 creator ticket (which makes sense), give ticket creatoruserid refer john. in case, john doesn't need in junction table, , don't need type column more.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -