I have a table called ' guests ' with these columns:
I need to select all the guests from a certain wedding_id
and left join the actual inviter fname
and lname
instead of inviter_id
(which corresponds with the guest id).
I got this far:
SELECT u.fname fname, u.lname lname, r.fname inviter_f, r.lname inviter_l
FROM guests u LEFT JOIN guests r ON u.inviter_id = r.guest_id
The only piece of the puzzle that I'm missing is adding a:
where wedding_id=10
(to select results only for a specific wedding id).
After edit: The final query: SELECT u.fname fname, u.lname lname, r.fname inviter_f, r.lname inviter_l FROM guests u LEFT JOIN guests r ON u.inviter_id = r.guest_id where u.wedding_id=10
Now a follow up question:
How do I return empty ' ' values where there is no inviter_id(set as 0), at the current query it returns it as NULL? (inviter_f and inviter_l returns NULL)
试试这个查询:
SELECT u.fname fname, u.lname lname, r.fname inviter_f, r.lname inviter_l FROM guests u LEFT JOIN guests r ON u.inviter_id = r.guest_id WHERE u.wedding_id = 10
FROM guests u LEFT JOIN guests r ON u.inviter_id = r.guest_id and r.wedding_id = u.wedding_id where u.wedding_id = 10;
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.