我遇到了一个问题,我希望使用col5=2206将表中的所有行都取出来,然后选择包含与col5 (fi )相同的col2和col3组合的col2和col3的所有其他行。在下面的示例中,col2=2,col3=6)
我还希望将返回的行筛选为具有col5 (fi )中某些值的行。col5=4000)。
('101', '2', '6', '2009-12-31', '2206', 'Exempt', '0', '0', '0', '4', '5'),
('102', '2', '6', '2009-12-31', '4000', 'Exempt', '-1', '0', '0', '4', '5'),
('103', '2', '6', '2009-12-31', '1200', '', '1', '0', '0', '4', '5');我尝试过各种子查询语句。但不能像在一张桌子上那样工作。这是可能的还是我需要创建一个更大的脚本。
发布于 2015-02-13 09:38:52
你使用自我连接来实现你想要的。
Select t2.*
From table t1
Join table t2 on ( t2.col2 = t1.col2 and t2.col3 = t1.col3 )
Where t1.col5 = '2206'
And t2.col5 <> '2206' -- replace that with specific filters like t2.col5 = '4000'
;发布于 2015-02-13 09:41:02
最好使用联接,但您需要子查询,然后可以尝试以下操作:
select a.* from table a where
a.col2 in (select col2 from table where col5=a.col5)
and
a.col3 in (select col3 from table where col5=a.col5)
and a.col5=4000;发布于 2015-02-13 09:35:44
我建议您使用后端代码,而不是SQL,因为这可能会给您带来性能问题。
https://stackoverflow.com/questions/28496370
复制相似问题