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?
Thanks
Select *
from Tickets
Left Join Members On tickets.member_id = members.member_id
select * from tickets left outer join members on tickets.member_id = members.ticket_id
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.