suppose we have two tables users and products
table users has an accountBalance column
table users schema:
userId accountBalance .........
1 500 .........
2 45000 .........
3 4238827 .........
. . .........
. . .........
. . .........
table products has a price column
table products schema:
productId price .........
1 40000 .........
2 55000 .........
3 90000 .........
. . .........
. . .........
. . .........
these table don’t have any relation so i can’t join them by a common key. What i want to know is to find out what products each user can buy and format it as the expected result following:
the expected result is:
userId productIdsUserAffordToBuy
1 NUll
2 1*2
3 1*2*3
. .
. .
. .
Using
GROUP_CONCAT()to create a list inside a single column and joining against the conditionaccountBalance >= priceyou can in fact perform aLEFT JOIN(necessary to returnNULLfor the user who can’t afford anything, rather than omitting the row):