Take the 2-minute tour ×

I am looking for some help with a MYSQL query. I have two tables, one contains a list of tickets and the other is a list of members.

Table Tickets: ticket_id, member_id Table Members: member_id

A member can but doesn't have to be assigned to a ticket. What I would like to do is select all the tickets and then if the member_id field is set in the tickets table fetch the member information as well.

One approach is to do a SELECT * FROM Tickets and then loop through in PHP and check if the member_id field is set. If set then do another query on the members table to fetch the corresponding information. The only problem with this is that it would be a large number of queries.

Is there any way to fetch all the results in one join query?


share|improve this question
add comment

4 Answers

up vote 1 down vote accepted
Select *
from Tickets
Left Join Members On tickets.member_id = members.member_id
share|improve this answer
add comment
select * from tickets left outer join members on tickets.member_id = members.ticket_id
share|improve this answer
add comment
SELECT t.ticket_id, t.member_id FROM tikcets t LEFT JOIN members m ON t.member_id = m.member_id

This will give you all the tickets whether they have been assigned to a member or not. You should add to the query the additional fields that you want to fetch.

share|improve this answer
add comment

Try this:

SELECT t.ticket_id, m.member_id
FROM tickets AS t LEFT OUTER JOIN members AS m 
ON t.member_id = m.member_id

The LEFT OUTER JOIN will cause all results from tickets to be returned, and any match from members, not disqualifying the ticket records, so this could do the trick.

share|improve this answer
Thanks for the explanation that is what i am after. –  ORStudios Nov 17 '11 at 14:07
add comment

Your Answer


