如何从SQL选择中排除某些行

时间:2018-11-23 09:59:10

标签: sql oracle

如何排除某些行? 例如,我有下表:

+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
|    1 | 1    |    R |
|    1 | 2    |    D |
|    2 | 3    |    R |
|    2 | 4    |    R |
|    3 | 5    |    R |
|    4 | 6    |    D |
+------+------+------+

我只需要选择:

|    2 | 3    |    R |  
|    2 | 4    |    R |  
|    3 | 5    |    R |  

我的选择无法正常运行:

with t (c1,c2,c3) as(
select 1 , 1 , 'R' from dual union all
select 1 , 2 , 'D' from dual union all
select 2 , 3 , 'R' from dual union all
select 2 , 4 , 'R' from dual union all
select 3 , 5 , 'R' from dual union all
select 4 , 6 , 'D' from dual), 
tt as (select t.*,count(*) over (partition by c1) cc from t ) select * from tt where cc=1 and c3='R';

谢谢!

7 个答案:

答案 0 :(得分:0)

使用row_number()窗口功能

    with t (c1,c2,c3) as(
select 1 , 1 , 'R' from dual union all
select 1 , 2 , 'D' from dual union all
select 2 , 3 , 'R' from dual union all
select 2 , 4 , 'R' from dual union all
select 3 , 5 , 'R' from dual union all
select 4 , 6 , 'D' from dual
    ),
  t1 as
  (
      select c1,c2,c3,row_number() over(order by c2) rn from t 
  ) select * from t1  where t1.rn>=3 and t1.rn<=5

demo link

C1  C2  C3
2   3   R
2   4   R
3   5   R

答案 1 :(得分:0)

这将起作用:

select * from (select * from table_name) where rownum<=4
minus 
select * from ( select * from table_name) where rownum<=2

答案 2 :(得分:0)

您可以尝试使用相关子查询

select * from tablename a
from 
where exists (select 1 tablename b where a.col1=b.col1 having count(*)>1)

答案 3 :(得分:0)

select * from table where col2 = 'R'

或者如果您只想排除D值的行

select * from table where col2 != 'D'

答案 4 :(得分:0)

这取决于您的要求,但是您可以采用以下方式:

if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {}

如果您想排除,只需像WHERE col1!= 1

您也可以使用IN子句,例如

SELECT * FROM `table` WHERE col1 = 2 AND col3 = "R"

此语法适用于MySql,但您可以根据自己的要求或所使用的数据库对其进行修改。

答案 5 :(得分:0)

我的猜测是,您希望col1的所有行都没有col1 = D的行,而col1 = R至少有1行。@可能存在[not]

DROP TABLE T;
CREATE TABLE T
(Col1 NUMBER, Col2 NUMBER, Col3 VARCHAR(1));
INSERT INTO T VALUES (    1 , 1    ,    'R');
INSERT INTO T VALUES (       1 , 2    ,    'D'); 
INSERT INTO T VALUES (       2 , 3    ,    'R'); 
INSERT INTO T VALUES (       2 , 4    ,    'R'); 
INSERT INTO T VALUES (       3 , 5    ,    'R');
INSERT INTO T VALUES (       3 , 6    ,    'D');
INSERT INTO T VALUES (       4 , 5    ,    'X');
INSERT INTO T VALUES (       4 , 6    ,    'Y');
INSERT INTO T VALUES (       5 , 6    ,    'X');
INSERT INTO T VALUES (       5 , 5    ,    'R');
INSERT INTO T VALUES (       5 , 6    ,    'Y');

SELECT * 
FROM T
WHERE NOT EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'D') AND
      EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'R');

结果

      COL1       COL2 COL3
---------- ---------- ----
         5          6 X    
         5          5 R    
         5          6 Y    
         2          3 R    
         2          4 R   

答案 6 :(得分:0)

根据您提供的信息,我只能推测,唯一的要求是COL1等于2或3,在这种情况下,您要做的就是(假设您实际上有表);

SELECT * FROM <table_name>
WHERE col1 IN (2,3);

这将为问题中提供的特定示例提供所需的输出。如果有一个选择要求超出了检索数据的范围(第1列是2或3),则可以提供更具体或更精确的答案。