sql - 根据群组成员资格选择
问题描述
假设我有两张桌子t
和o
. 我想选择t
其中的所有行t.feature = o.feature
(前两行)。
此外,我还想选择之前选择的行中t.grouping
=t.grouping
的所有行。在这种情况下1 , t.grouping
2 ergo rows('E', 1)
和. 实现这一目标的最优雅的方法是什么?('F', 2)
('G', 1)
CREATE TABLE t
( feature VARCHAR,
grouping BIGINT
);
INSERT INTO t (feature, grouping)
VALUES ('A', 1),
('B', 2),
('C', 5),
('D', 4),
('E', 1),
('F', 2),
('G', 1);
CREATE TABLE o
( feature VARCHAR
);
INSERT INTO o (feature)
VALUES ('A'),
('B');
解决方案
如果我理解正确,试试这个...
with cte as
(select grouping from t inner join o on t.feature=o.feature)
select t.*
from t
inner join cte on t.grouping=cte.grouping
或者
select
*
from t
where grouping in
(select grouping from t inner join o on t.feature=o.feature )
推荐阅读
- ruby-on-rails - Ruby on Rails 每天用抓取的数据填充数据库
- java - @Query 值可以包含多个查询吗?
- ubuntu - 我无法在我的 ubunutu 服务器上运行 nginx,我该如何解决?
- r - 更改数据结构以在 ggplot 中创建正确的条形图
- c++ - 编译模板函数时形参列表不匹配
- apache - Solr系统中如何修复错误的方法
- node.js - 摩卡在第一次等待电话后退出
- javascript - 如何在 Reactjs App 上安装 LESS css?
- sql-server - SQL Server 根据模板或现有表创建具有不同表名的动态表
- java - 从 json 响应中排除空对象、空数组和空对象数组