Answer the question
In order to leave comments, you need to log in
How to correctly form an sql query with an n: n connection?
There are 3 tables:
users , wallets , wallet_address .
A user may or may not have a value of some addresses to wallets .
A relationship of users to wallets is many to many .
wallet_address, in addition to the user_id and wallet_id fields, there is also value (namely, the wallet address itself)
The problem is that I can’t figure out how to generate an sql query to select all wallets in the system and, provided that the user has an address to the wallet, display this address. For example, the output of all application wallets:
foreach($wallets as $wallet) {
echo $wallet['name'];
echo $wallet['wallet_address'] //конкретный адрес , который добавил юзер(если нет , то null)
}
Answer the question
In order to leave comments, you need to log in
What's the problem?
users is one set;
wallets is another set;
wallet_address - intersection of sets (table with chords [user_id, wallet_id]).
You just need to glue all the tables according to the corresponding keys and display those records from users that intersect with wallets records with a non-empty value.
select u.user_id, w.wallet_id, wa.value as address
from users u
join wallet_address wa on wa.user_id = u.user_id
join wallets w on w.wallet_id = wa.wallet_id
where wa.value is not null
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question