sql - 如何从一个表中选择一个值并将其用于另一个选择?
问题描述
我有 2 张桌子,fe:
用户表:姓名,姓氏,user_id,...
对象表:用户 ID、日期、对象 ID、...
现在我想使用给定的变量Object.ID
并Object.Releasedate
从 Objecttable 中选择 userID,其中 date = Object.Releasedate 和 object_id = Object.ID。接下来,选择的用户ID应该用于从Usertable中选择名称,姓氏,其中user_id = userID。
我如何在一份声明中写下这个?
解决方案
内连接的替代方法是子查询
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
);
哪个更好取决于您的数据结构,如果您想知道,请查看说明计划。
推荐阅读
- node.js - 错误:找不到模块 '/var/app/current/NodeJS/index.js' + 502 Bad Gateway 错误
- python - 文本解析
- oracle - 触发预言机:怎么了?
- sql-server - 在 SQL Server 中,如何让 paymentdate 大于月份和年份而忽略日期部分?
- javascript - Three.js 鼠标滚轮向上/向下移动相机而不是放大/缩小
- arrays - [] 和 null 之间的实际区别
- javascript - 使用按钮将值复制到另一个输入
- python-3.x - 如何使训练神经网络的特征向量大小相等?
- powershell - 从 powershell 中的环境变量获取内置帐户
- timeout - Typescript 虚拟助手的 botbuilder-timeout