sql - 使用连接从嵌套选择中选择
问题描述
用户向我发送任意 SQL(我对此有所了解,我知道它有 ID 列),我需要能够过滤到在不同表中有条目的记录。
即我想做这样的事情
select * from (<user sql>) join t1 on t1.MemberID = ID where t1.Key = '12345'
这可行,但我的结果集最终包含 t1 的所有列以及用户列。我只想要用户提供的查询的列
解决方案
我建议使用表别名和exists
-- 并且只选择你想要的列:
select t.*
from (<user sql>) t
where exists (select 1
from t1
where t1.MemberID = t.ID and t1.Key = '12345'
);
为什么exists
?如果t1
碰巧有任何重复,这可以防止重复。
推荐阅读
- c# - httpwebrequest 给出错误的结果
- java - 如何在 void 函数中获取 System.out.print 的值并在另一个函数中使用它?
- java - Spring Boot 创建一个主题并发布,以便APPS可以接收它
- html - 使用 col 类的引导问题
- sql - 将两列合并/合并为 1 ( t-sql )
- ajax - 值不能为空。参数名称实体
- c# - 我可以有一个带有不确定设置的 TreeListView 仅以编程方式断言(并且拒绝用户)吗?
- collision-detection - 快速飞行小物体的碰撞检测
- excel - VBA宏基于一行中的多个值合并重复行
- arrays - 修改继承自`Array`的类的接收者