排除包含特定值的行

时间:2018-10-31 22:22:41

标签: sql oracle

我想排除加入特定小组的人。例如,如果一些学生注册了乐团俱乐部,而我想检索未注册乐团的学生名单,我该怎么办?

我无法简单地执行Group By子句,因为有些学生可能已经加入了多个俱乐部,并且会绕过Where条件并仍然显示在查询中, as shown here.

我正在考虑在SELECT子句中使用CASE语句将加入该乐队的人标记为“ 1”,如果尚未加入,则将其标记为“ 0”,但是我正在努力编写一个汇总的CASE函数,这会导致GROUP BY子句出现问题。

关于如何标记具有特定行值的人的任何想法?

显然我的表没有保存到SQLFiddle上,因此您可以将以下代码粘贴到自己的屏幕上:

CREATE TABLE activity ( PersonId, Club) as
  select 1, 'Soccer' from dual union
  select 1, 'Orchestra'  from dual union
  select 2, 'Soccer' from dual union
  select 2, 'Chess' from dual union
  select 2, 'Bball'  from dual union
  select 3, 'Orchestra'  from dual union
  select 3, 'Chess' from dual union
  select 3, 'Bball'  from dual union
  select 4, 'Soccer' from dual union
  select 4, 'Bball' from dual union
  select 4, 'Chess'  from dual;

4 个答案:

答案 0 :(得分:1)

使用带有大小写表达式的HAVING子句代替WHERE:

HAVING max(case when column = ‘string’ then 1 else 0 end) = 0

在分组后,添加此内容。

答案 1 :(得分:1)

如何从活动表中选择用户ID列表并将其排除:

SELECT * FROM users WHERE id NOT IN 
    (SELECT PersonId FROM activity WHERE Club = 'Orchestra');

答案 2 :(得分:0)

select * from 
(
  select a.*,  case when Club ='Orchestra' then 1 else 0 end flag
  from activity a 
) where flag =1; --> get some students signed up for an Orchestra club 

  select * from 
(
  select a.*,  case when Club ='Orchestra' then 1 else 0 end flag
  from activity a 
) where flag =0; --> get students not signed up for an Orchestra club

答案 3 :(得分:0)

您可以使用子查询返回要排除的人员列表。

<script src="https://www.gstatic.com/firebasejs/5.5.9/firebase.js"></script>
<script>
  // Initialize Firebase
  // TODO: Replace with your project's customized code snippet
  var config = {
    apiKey: "<API_KEY>",
    authDomain: "<PROJECT_ID>.firebaseapp.com",
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
    projectId: "<PROJECT_ID>",
    storageBucket: "<BUCKET>.appspot.com",
    messagingSenderId: "<SENDER_ID>",
  };
  firebase.initializeApp(config);

  //Sign in with Google
  var provider = new firebase.auth.GoogleAuthProvider();

  firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Google Access Token. You can use it to access the Google API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  console.log(`UserId: ${user.uid}`)

  // Try reading Firestore docs
  var db = firebase.firestore();

    // Disable deprecated features
    db.settings({
      timestampsInSnapshots: true
    });

    // This doc has members.[my-uid].role = "OWNER"
    // Read should succeed
    db.doc("bands/abc").get().then((doc) => {
            console.log(`${doc.id} => ${doc.data().name}`);
    });

    // This doc has members.[my-uid].role = "BANDMEMBER"
    // Read should fail
    db.doc("bands/def").get().then((doc) => {
            console.log(`${doc.id} => ${doc.data().name}`);
    });
  // ...
}).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential;
  // ...
  console.log('error login');
});
</script>

编辑:在子查询中删除了多余的字符-谢谢@mathguy。