sql - 如何定义表名并进行多表选择
问题描述
我需要在 SQL 的查询中使用多个表选择。但是如何引用查询中选择的表呢?
例如:(伪代码)
create table C as
select distinct id, product_code
from (
select distinct id, product_code
from A where dt = '2019-06-01'
)
inner join B on (select distinct id, product_code
from A where dt='2019-06-01').id = B.id;
上面的代码可能是错误的,但关键是表 A 不能直接使用,因为它太大了,必须指定 dt 是某个特定值。(所以我需要从 A 中选择两次以上)。我需要将较小的 A' 与其他表 B 进行内部连接。
例如,是否有可能“定义”表 A_ = select distinct blabla...from A ... 然后在查询中将 A_ 与 B 连接?
谢谢,
解决方案
您只需要一个表别名:
select distinct id, product_code
from (select distinct id, product_code
from table_A
where dt = '2019-06-01'
) a inner join
table_B b
on a.id = B.id;
推荐阅读
- c# - 我们可以将静态成员变量传递给构造函数来解决统一问题吗?
- c# - Google.Apis.Auth 1.46.0.0 已安装,但随后被要求提供多个版本
- r - RMarkdown 没有编译成 PDF
- r - 如何使用一种颜色创建微生物群数据的条形图以获得更高的分类等级和渐变颜色
- c++ - 使用 boost/asio 从网页获取特定数据
- compiler-construction - 编译器如何实现类型别名?
- java - 在 Java 中解析用户定义的格式
- c++ - Windows 64 位上的 ZwQueryInformationFile()
- c++ - 如何使用“等于”以外的评估编写 switch 语句
- java - JFrame:使用 JButton 导航到另一个框架