SQL Server:根据两列选择应用不同where条件的行

时间:2016-06-30 13:05:23

标签: sql database sql-server-2008

我列出了order_name中不同日期的订单供应商列表。

我想按照规则只选择订单:

  • 如果supplier_name:A,B,C order_date:2016-02-19
  • 如果supplier_name:P,Q,R order_date:2016-03-12
  • 如果supplier_name:M,N order_date:2016-03-15
  • 如果supplier_name:W,X,Y,Z order_date:2016-05-17
  • 以及2016-01-01
  • 的所有供应商记录的其余部分

列为order_datesupplier_nameaddresscost select * from tbl_supplier_order t1 where t1.order_date >= CONVERT(date, '2016-01-01') and (t1.order_date < CONVERT(date, '2016-02-19') AND t1.supplier_name not in ('A','B','C')) and (t1.order_date < CONVERT(date, '2016-03-12') AND t1.supplier_name not in ('P','Q','R')) and (t1.order_date < CONVERT(date, '2016-03-15') AND t1.supplier_name not in ('M','N')) and (t1.order_date < CONVERT(date, '2016-05-07') AND t1.supplier_name not in ('W','X','Y','Z'))

我最初的做法是:

$( "#prodBasketPaymOptIdRow1 td:nth-child(3)" ).text( "i want to set text here" );

2 个答案:

答案 0 :(得分:0)

你能准确说明你想要做什么吗?

If supplier_name: A,B,C order_date:2016-02-19

您是否说您的一个条件是您只想选择supplier_name为A,B,C且订单日期为2016-2-19的行,其他条件如下(可以是任何其他组合还有)?

答案 1 :(得分:0)

没有对此进行测试,但也许您可以尝试这样的事情。

select * from tbl_supplier_order t1 where
(t1.order_date>CONVERT(date,'2016-01-01') AND t1.supplier_name not in ('A','B','C','P','Q','R','M','N','W','X','Y','Z'))
OR (t1.order_date>CONVERT(date,'2016-02-19') AND t1.supplier_name in ('A','B','C'))
OR (t1.order_date>CONVERT(date,'2016-03-12') AND t1.supplier_name in ('P','Q','R'))
OR (t1.order_date>CONVERT(date,'2016-03-15') AND t1.supplier_name in ('M','N'))
OR (t1.order_date>CONVERT(date,'2016-05-17') AND t1.supplier_name in ('W','X','Y','Z'))