select both ways from mysql
i need to select all transactions for a particular user from a loans table
in mysql i have two tables loans and users
loans
id borrower_id lender_id amnt
1 1 2 10
2 1 3 5
3 2 1 2
for users
id name
1 bill
2 gates
3 microsoft
so if i select for bill using his id 1, i should get this expected output
output
id uid name borrower_id lender_id amnt
1 1 bill 1 2
2 1 bill 1 3
3 1 bill 2 1
as you can see in the loans table, a user can both borrow or lend loans
this is what i tried but am getting syntax errors
SELECT loans.*, users.name, users.id as uid
From
Left Join users On users.id = loans.lender_id
Left Join users On users.id = loans.borrower_id
Where
(loans.lender_id = 2 ) Or (loans.borrower_id = 2 );
what am i doing wrong?
No comments:
Post a Comment