假设我有这种MySQL数据库结构:
菜单:
id name
3 Tartiflette
4 Lasagne
成分:
id name price
15 Carrot 2.00
39 Garlic 3.00
40 Reblochon 5.00
55 Onion 1.00
菜单成分:
id ingredient_id menu_id quantity
4 15 4 2
5 55 4 1
6 39 4 1
7 40 3 2
8 55 3 3
Resa_menu:
id resa_id menu_id people
1 1 4 3
2 1 3 2
旅游:
id
1
如何使用一个带有items / endpoint的查询参数从一个预留中获取所有成分?
我尝试使用items / resa_menu?fields = *。*(和其他一些组合,但运气不好)的查询。
我还设置了一个SQL View(虚拟表)来获取查询所需的所有信息,但这种SQL似乎不是由Directus提取的。
SELECT
ingredient.name as ing_name,
(resa_menu.people * ingredient.price) as price,
(resa_menu.people * menu_ingredient.quantity) as quantity
FROM resa_menu
INNER JOIN menu ON menu.id = resa_menu.menu_id
INNER JOIN resa ON resa.id = resa_menu.resa_id
INNER JOIN menu_ingredient ON menu_ingredient.menu_id = menu.id
INNER JOIN ingredient ON ingredient.id = menu_ingredient.ingredient_id
GROUP BY ingredient.id;
预期产量:
ing_name price quantity
Carrot 6.00 6
Garlic 9.00 3
Reblochon 10.00 4
Onion 3.00 3
这里的具体目标是获得预订所需的所有成分的摘要(组合所有菜单,将数量乘以人数)。但我的问题更通用:如何使用Directus API在此查询中执行多个JOIN?
非常感谢您的帮助!
我想你的意思是你要求端点/items/Resa_menu
并且做*.*
正在返回Menu_ingredient
相关数据,但是你想要一个返回的成分列表?为此,你不只是将你的字段查询更改为*.*.*
以获得三个级别的关系?
或者是?fields=*.*
根本不工作?
使用左连接,你不需要分组
select i.name,(i.price* coalesce(rm.people,1)) as price,
(mi.quantity*coalesce(rm.people,1)) as quantity
from Ingredient i left join
Menu_ingredient mi on i.id =mi.ingredient_id
left join Resa_menu rm on mi.menu_id =rm.menu_id