我有一个问题,我希望使用 col5=2206
获取表中的所有行,然后选择包含与 col2 和 col3 相同的 col2 和 col3 组合的所有其他行col5(例如,在下面的示例中 col2=2,col3=6)
我还希望将返回的行过滤为在 col5 中具有特定值的行(例如 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');
我尝试过各种子查询语句。但无法让任何东西在一张 table 上工作。这是可能的还是我需要创建一个更大的脚本。
最佳答案
您使用自联接来实现您想要的。
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'
;
关于MySQL单表子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28496370/