简体   繁体   English

基于复杂条件的SQL

[英]SQL based on complex criteria

I'm struggling with SQL statement that I'm trying to write in MS Access and would appreciate any help. 我正在努力尝试在MS Access中编写SQL语句,希望对您有所帮助。

I have a table: 我有一张桌子:

 ***************************
 * attr1 * attr2 *  attr3  *
 ***************************
 *       *  A    *   1     *
 *       *  B    *   2     *
 *       *  B    *   3     *
 *       *  C    *   4     *
 *   B   *       *   1     *
 *   D   *       *   1     *
 *   A   *       *   2     *
 ***************************

I need result like this: 我需要这样的结果:

 ***************************
 * attr1 * attr2 *  attr3  *
 ***************************
 *   B   *       *   1     *
 *   D   *       *   1     *
 *   A   *       *   2     *
 *       *  B    *   3     *
 *       *  C    *   4     *
 ***************************

So, I need in my result all rows where attr1 is not null, and all other rows where attr3 has different values from those in rows where attr1 is not null. 因此,我需要在结果中所有attr1不为null的行以及所有其他attr3与attr1不为null的行具有不同值的行。

I can do it in Access in a way to make one query where I select all rows where attr1 is not null, than one "find unmatched" query where I select all rows with values in attr3 which are not included in first query, and than make union of those two queries... but I need some more direct approach (one sql statement if possible). 我可以在Access中进行某种方式的查询:选择attr1不为null的所有行,然后选择“查找不匹配”的查询,其中选择attr3中所有值的行不包含在第一个查询中,然后合并这两个查询...但是我需要一些更直接的方法(如果可能,一个sql语句)。

Thank you very much! 非常感谢你!

I think you probably need something like this: 我认为您可能需要这样的东西:

Select attr1, attr2, attr3
from table
where attr1 is not null 
      or attr3 not in (select attr3 from table where attr1 is not null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM