Skip to content
Advertisement

Using parent SQL column in subquery

Good morning. I’m trying to pull the username of the user from the column in to_id. Is there. It’d be simple if I was just filtering on to_id, but I also need records from another column from_id. I’ve attempted doing a UNION to get around this issue, but it only pulls records from user.id 3 of course.

Does anyone happen to know a way around this? I’m somewhat new to writing complex SQL queries. Haven’t been able to figure much out from similar questions.

The result of that would be along the lines of:

Username is null due to the two Joins being AND. If it’s OR, the username will show up, but the rows will be there twice. Once with the to_id username, once with the from_id username.

Advertisement

Answer

So you have one users table for all payers and payees accounts and one transaction table with two ID columns (payer and payee)? You need to join the users table to the transaction table twice, once to get the payer info, once to get the payee info.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement