在where子句

时间:2018-10-29 06:51:01

标签: sql sql-server multiple-columns where-clause

我正在尝试执行以下查询,但是它不满足我在where子句中提到的条件。它必须首先选择36、48、60中的术语,如果该术语为48,则该值不应介于-107至-305之间;如果该术语为60,则该值不应介于0至87之间且不包含-1304,-1204,如果term是36,则值不应大于-300。但是,当我运行查询时,它给出了排除中提到的所有值。请帮助

select distinct
Market,
(select top 1 LDCAccountidentifier from siteidentification where siteoid=#final.siteoid) as CustAccNum,
SiteOID,
RtlrContractIdentifier,
ContractOID,
ContractType,
ContractStatus,
Term,
ProductCode,
SigningDate,
FlowStartDate,
FlowEndDate,
RenewalDate,
Dateadd(dd,(term*365/12),flowstartdate) as [FSD+Term],
datediff(dd,Dateadd(dd,(term*365/12),flowstartdate),RenewalDate) as DifferenceinDays,
UsageFrom,
UsageTo,
DealFSD,
DealFED,
ContractUpdateDate,
UsageUpdateDate,
DealUpdateDate,
replace(replace(ErrorMessage, char(13),', '), Char(10),'') as ErrorMessage
from #final
where term in(36,48,60)
or (term=48 and datediff(dd,Dateadd(dd,1460,flowstartdate),RenewalDate) not between -107 and -305)
or (term=60 and datediff(dd,Dateadd(dd,1825,flowstartdate),RenewalDate)not between 0 and 87)
or (term=60 and datediff(dd,Dateadd(dd,1825,flowstartdate),RenewalDate) not in(-1304,-1204))
or (term=36 and datediff(dd,Dateadd(dd,1095,flowstartdate),RenewalDate)<-300)

2 个答案:

答案 0 :(得分:0)

您可以在下面使用花括号尝试设置OR运算符的优先级

add_action( 'woocommerce_process_product_meta', 'cfwc_save_sample_product_id' );
function cfwc_save_sample_product_id( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST['custom_text_field_sample_product_id'] ) ? 
    $_POST['custom_text_field_sample_product_id'] : '';
    $product->update_meta_data( 'custom_text_field_sample_product_id', sanitize_text_field( $title ) );
    $product->save();
}

add_filter( 'woocommerce_add_to_cart_validation',  'only_four_items_allowed_add_to_cart', 10, 3 );
function only_four_items_allowed_add_to_cart( $passed, $product_id, $quantity ) 
    {
    $cart_count = WC()->cart->get_cart_contents_count();
    $total_count = $cart_count + $quantity;
    if ( has_term( 'Sample Product','product_cat',$product_id ) && ( $cart_count >= 4 || $total_count > 2 ) ) {
        $passed = false; // Set to false
        $notice = __( "You Can add only 2 sample product , Can't Add More", "woocommerce" ); // Notice to display
    }

    if( ! $passed )
        wc_add_notice( $notice, 'error' );

    return $passed;
}

答案 1 :(得分:0)

上面的查询不正确,您应该使用WHEN-THEN条件才能获得所需的结果。

理想情况下,您应该在SQL中使用大小写。 如下所示:

SELECT
      CASE
        WHEN <Condition> AND <Condition>
            THEN
               --do your select query--
        WHEN <Condition> AND <Condition>
            THEN
                --do your select query-
        ELSE
            -- default task --
     END
  FROM <table name>