imgrooot Posted January 7, 2018 Share Posted January 7, 2018 (edited) Basically i want to match 2 columns from table1 to 1 column in table2. Here's my code. How do I do the joins properly? $get_earnings = $db->prepare("SELECT users.*, member_earnings.* FROM member_earnings LEFT JOIN users ON member_earnings.sent_by = users.user_id LEFT JOIN users ON member_earnings.guest_id = users.user_id WHERE record_id = :record_id AND status = :status ORDER BY earning_id DESC LIMIT 20"); $get_earnings->bindParam(':record_id', $url_id); $get_earnings->bindValue(':status', 1); $get_earnings->execute(); Edited January 7, 2018 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/306095-how-would-you-join-two-columns-in-the-same-table/ Share on other sites More sharing options...
kicken Posted January 7, 2018 Share Posted January 7, 2018 If you join the same table multiple times, you need to give the joins an alias so you can distinguish between them. SELECT sentBy.*, guest.*, member_earnings.* FROM member_earnings LEFT JOIN users as sentBy ON member_earnings.sent_by = sentBy.user_id LEFT JOIN users as guest ON member_earnings.guest_id = guest.user_id WHERE record_id = :record_id AND status = :status ORDER BY earning_id DESC LIMIT 20 Quote Link to comment https://forums.phpfreaks.com/topic/306095-how-would-you-join-two-columns-in-the-same-table/#findComment-1555124 Share on other sites More sharing options...
Solution imgrooot Posted January 7, 2018 Author Solution Share Posted January 7, 2018 If you join the same table multiple times, you need to give the joins an alias so you can distinguish between them. SELECT sentBy.*, guest.*, member_earnings.* FROM member_earnings LEFT JOIN users as sentBy ON member_earnings.sent_by = sentBy.user_id LEFT JOIN users as guest ON member_earnings.guest_id = guest.user_id WHERE record_id = :record_id AND status = :status ORDER BY earning_id DESC LIMIT 20 Perfect. That works like a charm. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/306095-how-would-you-join-two-columns-in-the-same-table/#findComment-1555125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.