简体   繁体   English

t-sql 2008 r2 where 语句有几个不是项目

[英]t-sql 2008 r2 where statement with several not items

In t-sql 2008 r2, I am trying to determine how to setup the sql to accomplish the following goal:在 t-sql 2008 r2 中,我试图确定如何设置 sql 以完成以下目标:

SELECT table1.customer_id, 
       type, 
       start_date, 
       end_date, 
       program_id 
FROM   table1 
       JOIN table2 
         ON table1.customer_id = table2.customer_id 
  1. where type not= ('aa','cc') and type not = 'g2' where code = 3 In table1 there are lots of records for each customer_id and there can be lots of various values for type. where type not= ('aa','cc') and type not = 'g2' where code = 3 在 table1 中,每个 customer_id 都有很多记录,并且类型可以有很多不同的值。 I only want the customer_ids that do not contain the values listed above.我只想要不包含上面列出的值的 customer_ids。 and
  2. table2 has only one customer_id. table2 只有一个 customer_id。 Customer_id is the key of table2. Customer_id 是table2 的key。 I want customers that do not have a value in one of the 3 columns: start_date, end_date, and program_id.我希望客户在以下 3 列之一中没有值:start_date、end_date 和 program_id。

Both parts 1 and 2 listed above need to be true for the customer_id to be selected.要选择 customer_id,上面列出的第 1 部分和第 2 部分都需要为真。 Thus can you tell me how to setup that sql?因此你能告诉我如何设置那个sql吗?

I cannot tell you exactly because I cannot see your database structure but I believe it would be something like this:我不能确切地告诉你,因为我看不到你的数据库结构,但我相信它会是这样的:

   SELECT table1.customer_id,type,start_date,end_date,Program_id
    FROM table1 JOIN table2 ON table1.customer_id = table2.customer_id
    WHERE (table1.type NOT IN('aa', 'cc', 'g2') AND table1.code = 3)
    AND (table2.start_data IS NULL OR table2.end_date IS NULL OR table2.program_id IS NULL)

Give the check to Jason(+1) but I would pull conditions into the join将支票交给 Jason(+1) 但我会将条件加入到连接中
Sometimes that helps the query optimizer有时这有助于查询优化器

SELECT table1.customer_id,type,start_date,end_date,Program_id
  FROM table1 
  JOIN table2 
    ON table1.customer_id = table2.customer_id
   AND table1.type NOT IN ('aa', 'cc') 
   AND (table1.code = 3 and table1.type <> 'g2')
   AND (table2.start_data IS NULL OR table2.end_date IS NULL OR table2.program_id IS NULL)

I get this is what you are literally asking for but it is not what you think it is我明白这就是你真正要求的,但这不是你认为的

AND table1.type NOT IN ('aa', 'cc') 
AND (table1.code = 3 and table1.type <> 'g2')

is the same as是相同的

AND table1.type NOT IN ('aa', 'cc')
AND table1.type <> 'g2' 
AND table1.code = 3 

is the same as是相同的

AND table1.type NOT IN ('aa', 'cc', 'g2')
AND table1.code = 3

I think what you mean to ask for is something like我想你的意思是要求是这样的

AND (    table1.type NOT IN ('aa', 'cc') 
      or (table1.code = 3 and table1.type <> 'g2') 
    )

How about trying to use the INNER JOIN:尝试使用 INNER JOIN 怎么样:

SELECT table1.customer_id,type,start_date,end_date,Program_id
  FROM table1 
INNER JOIN table2 ON table1.customer_id = table2.customer_id
  WHERE table1.type != 'aa' 
    AND  table1.type != 'cc' 
    AND table1.type != 'g2'
    AND table1.code = 3
   AND (table2.start_data IS NULL OR table2.end_date IS NULL OR table2.program_id IS NULL)

Let me know if it works fine with you!让我知道它是否适合你!

Regrads, ANDOURA Regrads, ANDOURA

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

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