Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-基于两种不同关系的筛选器选择行_Sql_Postgresql - Fatal编程技术网

SQL-基于两种不同关系的筛选器选择行

SQL-基于两种不同关系的筛选器选择行,sql,postgresql,Sql,Postgresql,我正试图为我的用户创建一个每日菜单,基于我今天为他们的中心提供的所有膳食。以下是我的数据库的外观: 下面是我希望查询检索的内容: Give me the emails of the users who have daily_email set to True with the information of their company and the hub their company belongs to. For each user, I need to see the meals ava

我正试图为我的用户创建一个每日菜单,基于我今天为他们的中心提供的所有膳食。以下是我的数据库的外观:

下面是我希望查询检索的内容:

Give me the emails of the users who have daily_email set to True with the information of their company and the hub their company belongs to. 
For each user, I need to see the meals available for them along with the information of the restaurant this meal belongs to.
到目前为止,我已经尝试使用内部联接并将所有表混合在一起,然后执行我想要的过滤器。它给了我我想要的,但表现糟糕。我主要不是在寻找最优的解决方案,而是至少一个性能可以接受的解决方案


任何帮助都将不胜感激。

除了根据表之间的关系连接所有表之外,没有其他方法:

select u.email, c.name, h.name, m.name, r.name
from users u
inner join companies c on c.id = u.company_id
inner join hubs h on h.id = c.hub_id
inner join hubs_meals hm on hm.hub_id = h.id
inner join meals m on m.id = hm.meal_id
inner join restaurants r on r.id = m.restaurant_id
where u.daily_email

确保所有外键都已编入索引。

谢谢,这至少比我拥有的要好。我还在末尾添加了另一个条件来过滤日期。谢谢!