首页 > 解决方案 > 如何从一个表中选择一个值并将其用于另一个选择?

问题描述

我有 2 张桌子,fe:

用户表:姓名,姓氏,user_id,...

对象表:用户 ID、日期、对象 ID、...

现在我想使用给定的变量Object.IDObject.Releasedate从 Objecttable 中选择 userID,其中 date = Object.Releasedate 和 object_id = Object.ID。接下来,选择的用户ID应该用于从Usertable中选择名称,姓氏,其中user_id = userID。

我如何在一份声明中写下这个?

标签: sql

解决方案


内连接的替代方法是子查询

select ut.name, ut.surname 
from Usertable ut 
where ut.user_id in (select ot.userID 
                     from Objecttable ot 
                     where ot.date = Object.Releasedate and
                           ot.object_id = Object.ID
                    );

哪个更好取决于您的数据结构,如果您想知道,请查看说明计划。


推荐阅读