Hi, I’m currently having a weird result when making a subquery within a subquery.
My current data model is the following :
I have a “items” collection storing my products and my customers.
I have a “relationships” collection with a “from” and a “to” field. (My real business case is much more complex but this is an exemple to reproduce the issue)
I want to display all my products and their customers :
[
{
"id": "1",
"name": "Product 1",
"customers" : [
{
"id": "11",
"name": "Customer1"
}
]
},
{
"id": "2",
"name": "Product 2",
"customers": [
{
"id": "12",
"name": "Customer2"
}
]
}
...
]
Here is the query that I made that doesn’t work :
SELECT product.id as id, product.name as name,
(
SELECT customer.id AS id, customer.name AS name
FROM `items` customer
USE KEYS (
SELECT RAW `to`
FROM `relationships`
WHERE `from`=product.id)) AS customers
FROM `items` product
This request gives me the following result :
[
{
"id": "1",
"name": "Product 1",
"customers" : [
{
"id": "11",
"name": "Customer1"
}
]
},
{
"id": "2",
"name": "Product 2",
"customers": [
{
"id": "11",
"name": "Customer1"
}
]
}
...
]
If I re run the request I might have the Product 2 returned first and then all the products will have customer 2 instead…
However if I build my query this way I get the correct result :
SELECT product.id as id, product.name as name, customers
FROM `items` product
LET customerKeys = (SELECT RAW `to`
FROM `relationships`
WHERE `from`=product.id),
customers = (SELECT customer.id AS id, customer.name AS name FROM `items` customer
USE KEYS customerKeys)
Seems to me that the first request should also be working but am I missing something ?